简体   繁体   English

iOS后退按钮标题未更改

[英]iOS Back button title is not changing

I have an issue with "back" button in UINavigationController 我在UINavigationController “返回”按钮有问题

I have a root view controller with method implemented like this: 我有一个根视图控制器,其实现方式如下:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"NEW BACK" style:UIBarButtonItemStylePlain target:nil action:nil];
}

Despite of this code, my back button title is still a title of my root view controller instead of "NEW BACK" 尽管有此代码,但后退按钮标题仍是我的根视图控制器的标题,而不是“ NEW BACK”

Does anyone sees where is the problem? 有人看到问题出在哪里吗?

Hide your default back button then add a button programatically. 隐藏默认的后退按钮,然后以编程方式添加按钮。 Add the code in your viewDidLoad. 将代码添加到您的viewDidLoad中。

self.navigationItem.hidesBackButton = YES;

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

        [backButton setBackgroundImage:[UIImage imageNamed:@"arrowleft.png"] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(btnBackTapped:) forControlEvents:UIControlEventTouchUpInside];
        backButton.frame = CGRectMake(0.0f, 0.0f, 25.0f, 20.0f);
        UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

        self.navigationItem.leftBarButtonItem = backButtonItem;

Add

btnBackTapped btnBackTapped

method anywhere in same class 同一类中任何地方的方法

-(void)btnBackTapped:(id)sender 
{
   [self.navigationController popViewControllerAnimated:YES];
}

Instead of doing this 而不是这样做

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"NEW BACK" style:UIBarButtonItemStylePlain target:nil action:nil];

in your RootViewController , do this in the child view controller means the controller which you are pushing. RootViewController ,在子视图控制器中执行此操作意味着您要推送的控制器。

If you set navigation controller's back button, then it will be applied to all the further viewController, though viewController has its own back button configuration. 如果设置了导航控制器的后退按钮,则它将被应用到所有其他viewController中,尽管viewController具有自己的后退按钮配置。 Try to reset the back button in your navigationController. 尝试重置您的navigationController中的后退按钮。 Hope your code will work. 希望您的代码能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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