Question

I have a UINavigationController that gets pushed a DetailsViewController. In this DetailsViewController, I want to use the toolbar that comes with every UINavigationController (atleast, since iPhone OS3.0).

So, in viewDidLoad in my DetailsViewController I create a UIBarButtonItem, I add it to an array and hand it off to the navigation controller:

- (void) viewDidLoad {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(selectTemplate)];  
    NSArray *items = [NSArray arrayWithObject: item];

    TestUIAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    UINavigationController *navController = delegate.navigationController;

    [navController setToolbarItems: items animated:NO];     
    [navController setToolbarHidden: NO animated: YES]; 
}

But, for some reason, while the UIToolbar is animated on to screen, the item is not added to the toolbar.

Is there some sort of specific order things have to be done with the UIToolbar for this to work?

P.S.: the application is in (forced) landscape mode and the navigationController.view has a rotation transform on it. Could that have anything to do with it ?

Was it helpful?

Solution

Have done some more digging and debugging and I've come to the conclusion that my approach of trying to modify the navigationController was wrong. Instead I should've simply set the toolbarItems property of the DetailsViewController.

After that, my code worked fine:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks target:self action:@selector(selectTemplate)];

        NSArray *myToolbarItems = [[NSArray alloc] initWithObjects: item, nil];         
        [self setToolbarItems: myToolbarItems];
        [myToolbarItems release];

    }
    return self;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top