UINavigationController “back button” custom text?

后端 未结 16 1493
Happy的楠姐
Happy的楠姐 2020-11-28 01:41

The \"back button\" of a UINavigationController by default shows the title of the last view in the stack. Is there a way to have custom text in the back button

16条回答
  •  没有蜡笔的小新
    2020-11-28 02:08

    in your init method, add the following code:

    - (id)initWithStyle:(UITableViewStyle)style {
        if(self = [super init]) {
    
            //...
    
            UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" 
                                                 style:UIBarButtonItemStylePlain 
                                                target:self 
                                                action:@selector(goBack)];
            self.navigationItem.leftBarButtonItem = customBackButton;
            [customBackButton release];
    
            //...
        }
        return self;
    }
    

    then add a simple method, to allow viewcontroller dismissing:

    -(void)goBack {
        [self.navigationController popViewControllerAnimated:YES];  
    }
    

提交回复
热议问题