How to create backBarButtomItem with custom view for a UINavigationController

前端 未结 14 1213
余生分开走
余生分开走 2020-11-29 19:39

I have a UINavigationController into which I push several views. Inside viewDidLoad for one of these views I want to set the self.navigationI

14条回答
  •  情书的邮戳
    2020-11-29 20:11

    The navigationController's backBarButtonItem is set on the item whose title you're trying to affect.

    i.e. in Page 1's view controller, say, viewdidLoad:

    self.title = @"Page 1 of 4";
    
    self.navigationItem.backBarButtonItem =
    [[[UIBarButtonItem alloc] initWithTitle:@"Page 1"
                                      style:UIBarButtonItemStyleBordered
                                     target:nil
                                     action:nil] autorelease];
    

    You would not override this in Page 2.

    Documentation for UINavigationItem : backBarButtonItem makes this clear:

    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.

提交回复
热议问题