Adding a UIBarButtonItem programmatically to UINavigationBar

前端 未结 4 850
执念已碎
执念已碎 2021-01-07 19:21

I dropped in a UINavigationBar in UIInterfaceBuilder. I present this view modally and just want a UIBackBarButton to return to my last

相关标签:
4条回答
  • 2021-01-07 19:51

    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;
    
    0 讨论(0)
  • 2021-01-07 19:52

    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
    
    0 讨论(0)
  • 2021-01-07 19:55

    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.

    0 讨论(0)
  • 2021-01-07 20:02

    You may use this setters without creation new UIBarButtonItem:

    [self.navigationItem.leftBarButtonItem setAction:@selector(doBackButton:)];
    [self.navigationItem.leftBarButtonItem setTarget:self];
    
    0 讨论(0)
提交回复
热议问题