按下后退按钮时,前一屏幕上的导航标题消失

时间:2014-07-01 08:57:33

标签: ios objective-c title back-button navigationbar

我的导航栏标题有问题。我有2个屏幕,当我点击屏幕2上的后退按钮时它将返回到屏幕1,但屏幕1的标题消失。这是我的代码段

screen1 .m

    - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.title = @"title 1";
....

屏幕2 .m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.title = self.stringTitle;

    // change the back button and add an event handler
    self.navigationItem.hidesBackButton = YES;
    //set custom image to button if needed
    UIImage *backButtonImage = [UIImage imageNamed:@"btn_back.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:backButtonImage forState:UIControlStateNormal];
    button.frame = CGRectMake(-12, 2, backButtonImage.size.width, backButtonImage.size.height);
    [button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];

    UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, backButtonImage.size.width-15, backButtonImage.size.height)];
    [backButtonView addSubview:button];

    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
    self.navigationItem.leftBarButtonItem = customBarItem;

    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
....
}

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

我尝试了几个技巧来制作屏幕1的标题,并带有以下技巧:

  1. viewwillappear方法

    上设置标题
    - (void)viewWillAppear:(BOOL)animated{self.title = @"tes";}
    
  2. viewdidappear方法

    上设置标题
     - (void)viewDidAppear:(BOOL)animated{
     self.navigationController.navigationBar.topItem.title = @"YourTitle";
      self.title = @"tes";
      }
    
  3. 但标题仍然消失。请帮忙


    这是我移动到下一个屏幕的方式

    - (IBAction)btGenerateTokenAction:(id)sender {
        SofttokenResultController *controller = [[SofttokenResultController alloc] initWithNibName:@"SofttokenResultController" bundle:nil];
    
        controller.navigationController.navigationBar.backItem.title = @"Kembali";
        [ViewLoadingUtil animateToNextView:controller from:self :@""];
    }
    
    + (void) animateToNextView:(UIViewController *)controller from:(UIViewController *)fromController :(NSString *)navTitle{
    
        NSLog(@"animateToNextView called");
    
        [fromController.navigationController pushViewController:controller animated:YES];
    }
    

7 个答案:

答案 0 :(得分:7)

这解决了我的问题:

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated: animated)
        self.navigationItem.title="Title text"
}

答案 1 :(得分:0)

viewWillAppear中执行此操作:

self.navigationItem.title="Title text";

答案 2 :(得分:0)

通常在你设置 backItem?.title = "" 时发生,因为后台控制器的标题是从这个属性中获取的 我从几个月来就遇到了这个问题,只是通过评论这一行就解决了

答案 3 :(得分:-1)

Objective c解决方案:

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];
    self.navigationItem.title = @"Title text";
}

答案 4 :(得分:-1)

就我而言,UINavigationController就是问题所在。在我的视图控制器和func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) { ... navigationController.navigationBar.backItem?.title = "" } 的自定义子类中,我将其设置为空字符串:

popViewController(animated:)

我的猜测是,当使用backItem?.title时,标题取自$.ajax({ url: 'xxx.php', data:formData, cache: false, contentType:false, processData:false, type: 'post', success: function(response) { //do something } 并设置为主标题属性。

答案 5 :(得分:-1)

为我工作解决方案。你可以尝试这个:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    self.navigationController?.navigationBar.topItem?.title = ""
    self.navigationItem.title = "Title"
}

答案 6 :(得分:-2)

我来自斯威夫特世界。但是我试了很多解决方案。这些是由动画引起的。如果将Bool更改为false,它将解决您的问题。最后,您可以将代码放在viewWillAppear函数中。

override func viewWillAppear(animated: Bool) {
    if animated {
        //...
    }
}