1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Accessing the app delegate

Discussion in 'iPhone/iPad Programming' started by beardedpig, Dec 17, 2008.

  1. macrumors member

    #1
    This is driving me potty and I just can't figure out where I am going wrong!

    Lets say I have an app delegate and three classes:

    AppDelegate, Class1, Class2 and Class3

    In AppDelegate I have an NSArray instance variable called ....Barry (lol)
    I also have in AppDelegate an NSUinteger outside of the interface block to hold the value of an indexPath from a table call.

    In Class1 I can import the app delegate .h file, create a reference to Barry using this reference and read and write its values.

    When I step through the code in debug I can see that instanceOfAppDelegate.Barry has been set and contains the one object Class1 writes to it.

    But if I then try the same thing in Class3 which is different from Class1 but needs to access Barry I do not get any errors but when querying instanceOfAppDelegate.Barry in Class3 it remains unset as it I dont think it is pointing to Barry in AppDelegate

    ???
     
  2. Moderator

    dejo

    Staff Member

    #2
    Maybe some code snippets will help us understand what you're doing.
     
  3. macrumors member

    #3
    good point!

    AppDelegate.h

    Code:
    #import <UIKit/UIKit.h>
    
    NSUInteger rowIndex;
    
    @interface AppDelegate : NSObject <UIApplicationDelegate> {
        
        UIWindow *window;
        UINavigationController *navigationController;
    	
        NSArray *Barry;
    }
    
    @property (nonatomic, retain) IBOutlet UIWindow *window;
    @property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
    @property (retain, nonatomic) NSArray *Barry;
    
    @end
    
    AppDelegate.m

    Code:
    #import "AppDelegate.h"
    #import "Class1.h"
    
    @implementation AppDelegate
    
    @synthesize window;
    @synthesize navigationController;
    @synthesize Barry;
    
    - (void)applicationDidFinishLaunching:(UIApplication *)application {
    	
    	// Configure and show the window
    	[window addSubview:[navigationController view]];
    	[window makeKeyAndVisible];
    }
    
    - (void)dealloc {
    	[navigationController release];
    	[window release];
    	[Barry release];
    	[super dealloc];
    }
    
    @end
    
    Class1.h

    Code:
    #import <UIKit/UIKit.h>
    
    @class AppDelegate;
    
    @interface Class1 : UITableViewController <UITableViewDelegate, UITableViewDataSource> 
    {	
    	AppDelegate *myAppDelegate;
    }
    
    @property (retain, nonatomic) AppDelegate *myAppDelegate;
    
    @end
    
    Class.m

    Code:
    myAppDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    
    rowIndex = [indexPath row];
    	 
    NSDictionary *rowData = [myAppDelegate.Barry objectAtIndex: rowIndex];
    	
    ....
    
    And here is the result of the German panel....beardedpig nil point!

    I had not placed this line in Class3

    Code:
    myAppDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    
    So by doing this little exercise I found my doofish error!!!!!

    Thanks ;-)))
     
  4. Moderator

    dejo

    Staff Member

    #4
    Glad I could help? :confused:
     

Share This Page