• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Two controllers in a window, how do I get one to run a function in another?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Two controllers in a window, how do I get one to run a function in another?


  • Subject: Re: Two controllers in a window, how do I get one to run a function in another?
  • From: BareFeet <email@hidden>
  • Date: Tue, 1 Sep 2009 12:25:54 +1000

Hi Graham and all,

I also tried to use bindings, which I've made work well for linking text views, table columns, even outline view columns, to data in my model. But I can't seem to set up bindings to link an instance variable in one controller to another controller. So I guess bindings are only for linking view objects to model objects, yes?

First, forget bindings... bindings is really an advanced topic, so I suggest you'll be better off leaving it alone for now. Bindings are mostly designed for handling the view<-->controller connections.

Actually, as I mentioned, I've had great success with bindings for linking view objects to data. I am actually surprised at how bindings seems to always be taught as an advanced topic, whereas I think it should be introduced much earlier to newbie Cocoa programmers, especially since it eliminates so much code, but that's another story.


I just wondered whether bindings could be used to link instance variables in two controllers, but I guess not.

In this case you have a controller<-->controller connection, which is likely to be a lot simpler.

You have two controllers. Each controller can have an IBOutlet to the other one. Just declare the outlets then wire them up in your nib, e.g:

I kinda got that, but missed what exactly to wire up...

// MyController.h

@class MyDocument;

@interface MController : NSObject
{
   IBOutlet MyDocument*    the_document;
}

...

@end

...
ahh, now that makes sense. That's what I was missing. I was trying to add IBOutlets for instance variables but needed to instead add IBOutlets for the class (eg MyDocument). Has this been documented anywhere? I couldn't find it. I would have thought it a common requirement, or is there a better approach to sharing data between controllers in the same nib?


When I try to compile the above, I get an error:

expected specifier-qualifier-list before 'MyDocument'

If I change the "MyDocument*" to "id", it works, but what's the problem?

Once you have added the outlets to your classes, they will be visible in IB, so just ctrl-drag from one object to the other and choose the outlet to connect it. In the .m files, #import the header for the other class:

#import "MyDocument.h"

Is this really necessary? It compiles without it. I did it anyway.

In your code, you can now do things like this, from your controller:

NSString* file = [the_document fileString];

the_document is showing as value nil. It doesn't seem to be linked to the instance of MyDocument. Am I missing something?


Thanks for all your help,
Tom
BareFeet

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Two controllers in a window, how do I get one to run a function in another?
      • From: Graham Cox <email@hidden>
References: 
 >Two controllers in a window, how do I get one to run a function in another? (From: Support <email@hidden>)
 >Re: Two controllers in a window, how do I get one to run a function in another? (From: Graham Cox <email@hidden>)
 >Re: Two controllers in a window, how do I get one to run a function in another? (From: BareFeet <email@hidden>)
 >Re: Two controllers in a window, how do I get one to run a function in another? (From: Graham Cox <email@hidden>)

  • Prev by Date: Re: Core Data observer exception in 10.6
  • Next by Date: Re: Two controllers in a window, how do I get one to run a function in another?
  • Previous by thread: Re: Two controllers in a window, how do I get one to run a function in another?
  • Next by thread: Re: Two controllers in a window, how do I get one to run a function in another?
  • Index(es):
    • Date
    • Thread