Search code examples
iphonexcodedelegatesinterface-builderibaction

Using Interface Builder efficiently


I am new to iPhone and objective c. I have spent hours and hours and hours reading documents and trying to understand how things work. I have RTFM or at least am in the process.

My main problem is that I want to understand how to specify where an event gets passed to and the only way I have been able to do it is by specifying delegates but I am certain there is an easier/quicker way in IB.

So, an example. Lets say I have 20 different views and view controllers and one MyAppDelegate. I want to be able to build all of these different Xib files in IB and add however many buttons and text fields and whatever and then specify that they all produce some event in the MyAppDelegate object. To do this I added a MyAppDelegate object in each view controller in IB's list view. Then I created an IBAction method in MyAppDelegate in XCode and went back to IB and linked all of the events to the MyAppDelegate object in each Xib file.

However when I tried running it it just crashed with a bad read exception.

My guess is that each Xib file is putting a MyAppDelegate object pointer that has nothing to do with the eventual MyAppDelegate adress that will actually be created at runtime.

So my question is...how can I do this?!!!


Solution

  • If you create an instance of MyAppDelegate in each nib file then, yes, you do end up with a lot of different instances of the class when all the nibs load. The app delegate is not identified by class or even protocol but rather by being the object pointed to by the application instance's delegate property. To find the true app delegate, you have have to ask the application object itself for its delegate

    You should have all your view controllers descend from a parent view controller class that has an appDelegate property. Implement something like this:

    #import "MyAppDelegateClass.h"
    
    @interface ViewControllerBaseClass :UIViewController {
        MyAppDelegateClass *appDelegate;
    }
    @property(nonatomic, retain)  *appDelegate;
    
    @end
    
    @implementation ViewControllerBaseClass
    @synthesize appDelegate;
    
    -(MyAppDelegateClass *) appDelegate{
        self.appDelegate=(MyAppDelegateClass *)[[UIApplication sharedInstance] delegate];
        return appDelegate;
    }
    @end
    

    When the view controller needs the app delegate it just calls self.appDelegate. If you want to access an attribute of the app delegate use self.appDelegate.attributeName.

    The important thing is that you ask the application for its specific delegate instance at runtime. You can't do that from a nib file.