Persistence and Scope

Discussion in 'iOS Programming' started by Kizmar, Nov 7, 2010.

  1. Kizmar macrumors newbie

    Kizmar

    Joined:
    Oct 28, 2010
    #1
    Again, .NET developer that's new to iOS/Mac-type development.

    I'm attempting to use a TabBar delegate method "didSelectViewController" to intercept a tab change and set a property on the view controller when a specific tab is set. Everything is "working" except the property's value appears to have lost persistence by the time I arrive at the view controller's "viewDidLoad" method.

    I need to back up a little bit and figure out what the "best practice" is when doing something like this. Should I be using a global variable instead of trying to set a property of the view controller?

    Or... am I doing something wrong in the declaration of the property? Here's what I did on the view controller header:
    Code:
    ...
    @interface EntityListViewController: UIViewController {
      @public
          NSString *listType;
          NSNumber *someID;
    }
    
    @property (retain) NSString *listType;
    @property (nonatomic, retain) NSNumber *someID;
    ...
    
    Then the .m file:
    Code:
    ...
      @synthesize listType;
      @synthesize someID;
    ...
    
    The delegate:
    Code:
    // Optional UITabBarControllerDelegate method.
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
       if (viewController.tabBarItem.tag == 2) {
           // Set a property of the currently selected view
           ((EntityListViewController *)viewController).listType = @"Testing";
        }
    }
    
    With this, by the time I grab the value of "listType" in the "viewDidLoad" method of EntityListViewController, it's empty.

    Just need a point in the right direction. Thanks. :)
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    I suspect things are happening in a different order than you think they are: viewDidLoad is most likely being called before tabBarController:didSelectViewController:. This makes sense if you think about it: tabBarController:shouldSelectViewController: is called before the tab is selected. The tab is then selected (which will load the view if required firing viewDidLoad). Then once the selection is completed tabBarController:didSelectViewController: is called.
     
  3. Kizmar thread starter macrumors newbie

    Kizmar

    Joined:
    Oct 28, 2010
    #3
    I didn't think about that. Can I get to "viewController.tabBarItem.tag" from the "viewDidLoad" method?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Maybe. I'm honestly not sure. I think so, but I'm not sure if viewDidLoad is called before or after the view is inserted into the view hierarchy. The best I can suggest is to give it a shot and see.
     
  5. Kizmar thread starter macrumors newbie

    Kizmar

    Joined:
    Oct 28, 2010
    #5
    Thanks for the troubleshooting ideas. I was stumped there. :)
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    If you are trying to customise the view based on which tab it is then look at the viewWillAppear: view controller method. This is the normal place to do that sort of thing.
     
  7. Kizmar thread starter macrumors newbie

    Kizmar

    Joined:
    Oct 28, 2010
    #7
    It appears that both "viewWillAppear" and "viewDidLoad" are hit before the tab bar delegate method "didSelectViewController".

    I'm at a loss here.

    All I'm trying to do is set a global variable between the "touch up inside" event of a tab bar item and the "viewWillAppear" or "viewDidLoad" methods. That or I need to interrogate the tab bar from the active view and see that the current tab's tag is. Can't figure that out either.

    Arg! :(
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    OK, but it's not really clear why? Can you not just set it when you set which view controller is assigned to each tab?
     
  9. Kizmar thread starter macrumors newbie

    Kizmar

    Joined:
    Oct 28, 2010
    #9
    This is where my lack of experience is killing me I think. I haven't quite figured out if/how I can use the Interface Builder to get this tab to fire off an action when it's selected (because it's the MainWindow.xib). That nib and the tab bar controller are still a bit of a black box to me.
     
  10. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    Does viewWillAppear: not do that? I.e. only get called when the view will appear as the tab is selected?
     
  11. Kizmar thread starter macrumors newbie

    Kizmar

    Joined:
    Oct 28, 2010
    #11
    So are you saying "viewWillAppear" will only fire off if the view is being made active as a result of a tab bar item? I haven't gotten far enough to test this if that is the case. Theoretically this view will end up being called from several different places within in the app (including the tab bar).
     
  12. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #12
    Why not use tabBarController:shouldSelectViewController:, which is a method already pointed out, and one that should be called BEFORE a view controller is selected.
     
  13. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #13
    viewWillAppear: is only called when the view will be presented on screen. So it should be called when that view becomes selected in a tab controller, when it becomes visible in a navigation stack (either you push it or the stack gets popped back to it) etc.
     
  14. Kizmar thread starter macrumors newbie

    Kizmar

    Joined:
    Oct 28, 2010
    #14
    Apparently I don't know *how* to use this method. As soon as I add it I end up with a "SIGABRT". AppDelegate.m...
    Code:
    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    	return YES;
    }
    
     
  15. Kizmar thread starter macrumors newbie

    Kizmar

    Joined:
    Oct 28, 2010
    #15
    I'm abandoning this. It's obviously way over my head. Thanks for trying to help.
     
  16. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #16
    Use the debugger. Start by setting a breakpoint.
     
  17. Kizmar thread starter macrumors newbie

    Kizmar

    Joined:
    Oct 28, 2010
    #17
    Yeah I tried all that. I don't know where to look for useful exception information when the app aborts and bails. Trying different methods. We'll see.
     

Share This Page