开发者

why do toolbar items not appear when I automatically jump to that view based on stored state?

开发者 https://www.devze.com 2023-02-25 18:50 出处:网络
I\'m trying to understand why when I automatically jump to a 2nd view (using UINavigationController) after startup and reviewing stored state, that the toolbar items do not appear?

I'm trying to understand why when I automatically jump to a 2nd view (using UINavigationController) after startup and reviewing stored state, that the toolbar items do not appear?

When I go back to main page (via UINavigationController standard arrangements), and then then select the row in the UITableView, and go back into the same view again the toolbar items appear fine.

Code extracts to give the rough idea is:

mainController - normal selection based entry

mainController - upon restart & checking if previous state user was in 2nd layer view

  • In bottom of viewDidLoad method check the state for previous view
  • If need to then automatically jump to 2nd layer view by following same method as per the normal selection approach above - in fact I refactored the code for both to use the same method/code for this

2nd Layer View

  • within ViewDidLoad setup the toolbar - code for this in this method

Code:

- (void)setupToolbar {
    [self.navigationController setToolbarHidden:NO];
    UIBarButtonItem *increaseFontButton = [[UIBarButtonItem alloc] 
                                           initWithImage:[UIImage imageNamed:@"icon_zoom_in.png"] 
                                           style:UIBarButtonItemStylePlain 
                                           target:self 
                                           action:@selector(pressButtonIncreaseFont:)
                                           ];
    UIBarButtonItem *decreaseFontButton = [[UIBarButtonItem alloc] 
                                           initWithImage:[UIImage imageNamed:@"icon_zoom_out.png"] 
                                           style:UIBarButtonItemStylePlain 
                                           target:self 
                                           action:@selector(pressButtonDecreaseFont:)
                                           ];
    NSArray *items = [NSArray arrayWithObjects: increaseFontButton, decreaseFontButton, nil];
    self.toolbarItems = items;

    //release buttons
    [increaseFontButton release];
    [decreaseFontButton release];

}

Any ideas? Ideas for fault finding?


One feature of Objective-C I find highly annoying and error-prone is the silent failure of calling a method on a null object. After your first line in the setupToolBar method, check if the navigationController is null:

  NSLog(@" navigationController is 0x%x", self.navigationController);

Is the navController created in the same manner for the restart case as in the regular case?


I worked out how to fix this through process of elimination, but I don't understand why :)

So what fixed it was changing the following line in the application delegate's "didFinishLaunchingWithOptions" method:

  // OLD Entry - Did not work
  //[self.window addSubview:navigationController.view];

  // NEW Entry - Fixed it
  self.window.rootViewController = self.navigationController;

Any ideas why?

0

精彩评论

暂无评论...
验证码 换一张
取 消