Sending variable from delegate to view controller.

Discussion in 'iOS Development' started by andydam123, Jul 19, 2009.

  1. andydam123

    andydam123 Member

    Joined:
    Aug 11, 2008
    Messages:
    447
    Likes Received:
    0
    I have an NSMutableArray in my delegate that read from a plist, it has @synthesis, @property, etc.

    But I want the contents of it to be in the view controller, so I have a NSArray in my view controller, with @synthesis and @property.

    How do I get the NSMutableArray in the delegate to go over to the NSArray in the view controller?
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Since we have no clue how your app is set up, we can not help you. Please elaborate more.
  3. bones237

    bones237 New Member

    Joined:
    Feb 17, 2008
    Messages:
    101
    Likes Received:
    7
    Device:
    iPhone 3G (White)
    if when you say "delegate" you mean your appDelegate then i would use UIApplication.

    First i would make that array in the view controller mutable (IMHO)
    then in your viewController's viewDidLoad or whatever.....
    [OBJC]
    YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
    self.yourViewControllerArray = appDelegate.yourDelegateArrayFromPlist;
    [/OBJC]
  4. andydam123

    andydam123 Member

    Joined:
    Aug 11, 2008
    Messages:
    447
    Likes Received:
    0
    I tried that, but it still doesn't work. It keeps on giving me a blank array.

    [OBJC]
    //The NSMutableArray in AppDelegate.m
    NSMutableArray *appsNames = [[NSMutableArray alloc] initWithObjects

    Please Register or Log in to view images

    "Example1", @"Example2", nil];
    [appsNames retain];
    [/objc]

    [objc]
    //The NSArray in RootViewController.m
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSArray *appsRoot = [[NSArray alloc] initWithArray:appDelegate.appsName];
    [/objc]
  5. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Just wondering, why are you allocating an array and then retaining it again? It is not an autorelease object that you're creating, so it wont be released until you bump the retain count down to 0 with -release. No need to retain in your case.
  6. bones237

    bones237 New Member

    Joined:
    Feb 17, 2008
    Messages:
    101
    Likes Received:
    7
    Device:
    iPhone 3G (White)
    Your code is not the same as what i did... I thought you said you set accessors of the array? You never pass the array you allocated to your delegate array?

    You had this....
    But you want this... (as long as you created accessors for "appDelegateArrayWithAccessors"....@property, @synthesize)

    [OBJC]
    NSMutableArray *appsNames = [[NSMutableArray alloc] initWithObjects

    Please Register or Log in to view images

    "Example1", @"Example2", nil];
    self.appDelegateArrayWithAccessors = appsNames;
    [appsNames release];
    [/OBJC]

    Then with above in AppDelegate.m and the following in your view controller
    [OBJC]
    YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSArray *tempArray = [NSArray arrayWithArray:appDelegate.appDelegateArrayWithAccessors];
    /*
    An alternative way to pass array over if you set accessors for your "tempArray" (NSArray) in the view controller would be this....

    self.tempArray = appDelegate.appDelegateArrayWithAccessors
    */
    [/OBJC]

    you should be fine....

    Remember to import you AppDelegate header into your view controller


    EDIT....
    I was just thinking...Is your view controller's view the view that gets added to the window upon launch?? If so then these things need to be in a certain place... For example, if it is the view that gets added to the window upon launch and you are trying to pass the array over to the view controller in the controller's viewDidLoad method then the code in the AppDelegate (where array is created and whatnot) would need to be in applicationDidFinishLaunching BEFORE the view controller's view is added to the window. Follow??
    Otherwise, yes, the view controller array would be empty because you passed it over before it was even created in the AppDelegate (viewDidLoad is called already)

    If this view controller's view is not the one added to the window upon launch then you have more leeway on where to put the code in the AppDelegate.
    Obviously you want to make sure the delegate has allocated memory for it before you try and point to it from another class.
  7. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    Last time I check, viewDidLoad called once because view controller is still in the memory.
  8. bones237

    bones237 New Member

    Joined:
    Feb 17, 2008
    Messages:
    101
    Likes Received:
    7
    Device:
    iPhone 3G (White)
    Yes, it is only called once....
    My above post still applies tho..

    I'm not sure i understand what you mean

Share This Page