简体   繁体   中英

Cannot add a right or left bar button item to my nav controller

Have tried all the examples on this website I just don't see anything on my modal view, I do see the navigationbar though but its empty

EditEntityViewController *editEntityViewController = [[EditEntityViewController alloc] init];
editEntityViewController.currentNode = newNode;
editEntityViewController.delegate = self;

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:editEntityViewController];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;


UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show"
                                                                  style:UIBarButtonItemStylePlain 
                                                                 target:self
                                                                 action:@selector(refreshPropertyList:)];          
editEntityViewController.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];


[self presentModalViewController:navController animated:YES];

[editEntityViewController release];

Try setting the rightBarButtonItem on editEntityViewController before you create the UINavigationController with initWithRootViewController:.

I think that the navigation bar is set up when the UINavigationController is created. Adding the right bar item after creation time is too late.

EDIT: Ok, so that's not the issue.

The following minimal code snippet works so I would check whether your EditEntityViewController is doing something to remove the button elsewhere:

- (IBAction)showPopup:(id)sender
{
    UIViewController *popupController = [[UIViewController alloc] init];

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:popupController];
    navController.modalPresentationStyle = UIModalPresentationFormSheet;
    navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;


    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show"
                                                                      style:UIBarButtonItemStylePlain 
                                                                     target:self
                                                                     action:nil];       

    popupController.navigationItem.rightBarButtonItem = anotherButton;
    [anotherButton release];

    [self presentModalViewController:navController animated:YES];

    [popupController release];
}

The reason why this was not working is really stupid. Basically I had an IBOutlet defined in EditViewController called navigationItem which was conflicting with the SDK's property with the same name.

I removed it and the link from the nib and as Robin says it works perfectly.

As discussed, your code was correct and is the standard way to show a popup sheet with a UINavigationBar to hold buttons to dismiss the sheet. However, you had defined an IBOutlet in EditViewController called navigationItem, which was causing a conflict.

Modally presented view controllers on navigation controllers don't have navigationItem nor navigationController properties. They DO, however, have parentViewController property, but this is irrelevant in your case.

If you want to customize navigation bar on your modally presented view, you should connect IBOutlet from view controller managing that view to the navigation bar placed in that managed view. Then do the manipulation through IBOutlet instance variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM