1

I need to set some UI elements of an UIViewController from AppDelegate. I use a Storyboard, so the UIViewController don't become initialized in AppDelegate.

I try to get the instance of UIViewController with:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
TableViewController* myTableViewController = [storyboard instantiateViewControllerWithIdentifier:@"myTableViewController"]; 

But when I try to set some UI element like the UIBarButtonItem of the UINavigationBar, like here:

UIBarButtonItem* refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshEntry)];
    myTableViewController.navigationItem.rightBarButtonItem = refreshButton;

the UINavigationBar stay blank and the UIBarButtonItem don't appear...

How can I change the UI?

Lindemann
  • 3,336
  • 3
  • 29
  • 27

1 Answers1

5

Unfortunately, as far as I can tell, there is no way with Storyboards to plug in to the app delegate. Therefore, you can't directly grab a handle to the view controller in your app delegate lifecycle methods to do some additional work.

However, you DO know that the app delegate's windows' rootViewController property will be the initial view controller you specify in your Storyboard. So you can always do something like this:

XXRootViewController *rootViewController = (XXRootViewController *)self.window.rootViewController;
[rootViewController doAdditionalWork];

Also, the reason why calling -instantiateViewControllerWithIdentifier: on your Storyboard didn't work is, well, because it's actually instantiating a new view controller. However, the view controller hierarchy you set up in your Storyboard file is what is actually unarchived when your app launches. So you're effectively instantiating that view controller, making some changes to it, and then it's being deallocated.

CIFilter
  • 8,647
  • 4
  • 46
  • 66
  • I found now, that I know what the problem was, a similar question: http://stackoverflow.com/questions/8784143/accessing-a-view-controller-created-through-storyboard-using-the-app-delegate – Lindemann Feb 20 '12 at 01:05