Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 26, 2012, 07:19 PM   #1
BadWolf13
macrumors 6502
 
Join Date: Dec 2009
Multiple XIBs connecting to the same Object?

Hey guys,

I'm working on an Objective C project with multiple XIBs. To keep this project simple, I've put all of the processing within the myProjectAppDelegate file. To make the connection of all the IBOutlets to the view objects, I added an NSObject to each XIB and set its class to myProjectAppDelegate. I thought that it would link them all to the same instance of the AppDelegate, but it hasn't. It appears that it's creating a different instance for each of the XIB files. My question is, is it possible to link all three XIBs to the same instance of myProjectAppDelegate, or any class for that matter?
BadWolf13 is offline   0 Reply With Quote
Old Jan 26, 2012, 08:36 PM   #2
Mac_Max
macrumors 6502
 
Join Date: Mar 2004
I believe the architecture of Objective C/Cocoa on the Mac doesn't allow for that. The idea is one controller instance per model. What you could do is write your controller so that every instance of it works from the same instance of your model (dirty way: singleton, better way: database of some kind with transactions and locks). There are a LOT of issues that can come up when writing data if not done properly. If you make it so that only one view can manipulate the data at a time, it makes life a lot easier. You also might want to look into data binding the view and model together. Databinding isn't a "normal" OS X development thing but can be done. Its really been helpful to me on the Windows side of things for displaying and editing data from multiple views or multiple parts of the window.


Cocoa Databinding:
http://cocoaconvert.net/2009/05/31/t...ding-in-cocoa/
Mac_Max is offline   0 Reply With Quote
Old Jan 26, 2012, 08:46 PM   #3
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Quote:
Originally Posted by BadWolf13 View Post
My question is, is it possible to link all three XIBs to the same instance of myProjectAppDelegate, or any class for that matter?
Yes. Set the class on each of your nib's File's Owner object. Don't add a new NSObject object into the nib. That will create a new instance of your object. File's Owner acts as a proxy to whatever is creating the nib in code.
kainjow is offline   0 Reply With Quote
Old Jan 26, 2012, 09:30 PM   #4
BadWolf13
Thread Starter
macrumors 6502
 
Join Date: Dec 2009
Quote:
Originally Posted by kainjow View Post
Yes. Set the class on each of your nib's File's Owner object. Don't add a new NSObject object into the nib. That will create a new instance of your object. File's Owner acts as a proxy to whatever is creating the nib in code.
Thanks, but I'm prevented from doing that right now. I've got the File's Owner set to NSViewController because I'm using the following line of code to load the XIB.

Code:
	basicController = [[NSViewController alloc] initWithNibName:@"Basic" bundle:nil];
I don't know of any other way to load a XIB file. Is there?
BadWolf13 is offline   0 Reply With Quote
Old Jan 26, 2012, 11:41 PM   #5
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
You can use +[NSBundle loadNibNamed:owner:]
kainjow is offline   0 Reply With Quote
Old Jan 27, 2012, 12:01 AM   #6
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
If you set the owner to the class of your app delegate in the nib file and specify your app delegate as the owner when you load the nib as kainjow suggests above, any outlets you define in your app delegate can be connected directly using the Files Owner in the nib itself. Create a different IBOutlet for each nib in your app delegate's ivars and the nib should connect directly to the appropriate outlet when it is instantiated.

When you add a NSObject to a nib, an instance of that object will be created when the nib is loaded, so your app delegate would propagate instead of the original being used.
__________________
You got to be a spirit. You can't be no ghost.
Sydde is offline   0 Reply With Quote
Old Jan 27, 2012, 12:14 AM   #7
rossipoo
macrumors regular
 
Join Date: Jun 2009
Wirelessly posted (Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3)

If you want to communicate back to the app delegate, one way is to add a delegate property to the view controller(s) and have the app delegate set itself as a delegate to the view controller during awakeFromNib. Then the view controller can call its delegate methods when it needs to communicate back to the app delegate.
rossipoo is offline   0 Reply With Quote
Old Jan 27, 2012, 03:32 PM   #8
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
What do want to do with the app delegate?

If you only want to connect actions to it, you can connect the actions to the First Responder pseudo-object. Your app delegate will be in the responder chain. It will be last in the responder chain though, so make the action unique application-wide to avoid an object earlier in the chain inadvertently responding to the action.

If you only want to pull properties from the app delegate, set up an app delegate property in whatever you're File's Owner is. You can get the app delegate anywhere in your code using [NSApp delegate].

If you want to use any of those app delegate properties in the UI, use Cocoa binding or setup the UI values in awakeFromNib:.
jiminaus is offline   0 Reply With Quote
Old Jan 27, 2012, 08:25 PM   #9
BadWolf13
Thread Starter
macrumors 6502
 
Join Date: Dec 2009
jiminaus, I'm using the AppDelegate class as my controller, in classic MVC structure. XCode always creates that class, and I barely use any of the app delegate features, so I figure, why not give use it as my controller as well?

I've got all my central processing and calculations happening in that class, with a model, and 3 XIBs. It's nicely organized.

Kainjow, thank you, that worked beautifully. I created an outlet for the custom view in each XIB, and was able to get rid of the view controllers. Now it works perfectly.
BadWolf13 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: How do I convert an NSData object to an NSDictionary object, and vice versa? moonman239 iPhone/iPad Programming 6 May 2, 2014 01:17 PM
Connecting multiple displays to new iMacs? .parallelogram. Mac Peripherals 6 Mar 16, 2014 05:45 AM
Connecting multiple Apple Tv for hotel hermitsethi Apple TV and Home Theater 3 Sep 25, 2012 01:32 PM
Connecting multiple time capsules JamesOK Mac Peripherals 5 Jul 14, 2012 09:48 PM
Help binding across .xibs in master/detail app GorillaPaws Mac Programming 4 Jun 23, 2012 11:38 PM

Forum Jump

All times are GMT -5. The time now is 01:15 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC