Adding a UIBarButtonItem programmatically to UINavigationBar

社会主义新天地 提交于 2019-11-30 21:50:17

问题


I dropped in a UINavigationBar in UIInterfaceBuilder. I present this view modally and just want a UIBackBarButton to return to my last view. I have an outlet and property to this UINavigationBar declared. I thought in my viewDidLoad method, I could create a UIBackButton like this:

UIBarButtonItem *backButton = 
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered 
                                              target:self 
                                              action:@selector(goBack)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];

But I do not see my UIBackBarButtonItem on the UINavigationBar. I think I am doing something wrong here since I don't think my UINavigationBar knows I'm trying to add this UIBackBarButtonItem to it in this way. Would I have to do create an NSArray, put the button in it, and setItems for the NavigationBar instead?

I'm confused on how the navigationItem property works vs the setItems of the UINavigationBar as well. Any help would be appreciated. Thanks!


回答1:


You are trying to set the Back Button Item in a modal view which doesn't add a backBarButtonItem. This what causes the Button (or any sort of back button for that matter) not to show. The backBarButtonItem is mainly for use with Pushed View Controllers which have a Back Button added from the parent (next item below) when you push a new view controller (top item). The Apple UINavigationItem Documentation says:

When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.

To get the Back Button on the left side like you wish, Try changing

self.navigationItem.backBarButtonItem = backButton;

to

self.navigationItem.leftBarButtonItem = backButton;



回答2:


making a call such as this from a view controller

{
    NextViewController* vcRootView = [[NextViewController alloc] initWithNibName:@"NextView" bundle:[NSBundle mainBundle]];
    UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vcRootView];
    [vcRootView release];

    [self.navigationController presentModalViewController:navController animated:YES];
    [navController release];    

}

will present NextViewController as a Modal view on the calling view and NextViewController will have a navigationController for it.

In The NextViewController implementation file all you need is this

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self 
                                                                  action:@selector(barButtonBackPressed:)];
    self.navigationItem.leftBarButtonItem = backButton;
    [backButton release];
}


-(void)barButtonBackPressed:(id)sender{
    [self dismissModalViewControllerAnimated:YES];
}

to have the back button to dismiss the modalview. Hope it helps.




回答3:


Use below code snippet :

//Add button to NavigationController
UIBarButtonItem *backButton = 
 [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@“back”, @"")
                                  style:UIBarButtonItemStylePlain 
                                 target:self 
                                 action:@selector(goBack)];

self.navigationItem.leftBarButtonItem = backButton;

//Perform action on back Button
- (void) goBack {    // Go back task over-here
}

Different style types available are :

UIBarButtonItemStylePlain, UIBarButtonItemStyleBordered, UIBarButtonItemStyleDone



回答4:


You may use this setters without creation new UIBarButtonItem:

[self.navigationItem.leftBarButtonItem setAction:@selector(doBackButton:)];
[self.navigationItem.leftBarButtonItem setTarget:self];


来源:https://stackoverflow.com/questions/6721169/adding-a-uibarbuttonitem-programmatically-to-uinavigationbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!