【问题标题】:navigation title on previous screen disappear when back button pressed按下后退按钮时,前一个屏幕上的导航标题消失
【发布时间】:2014-07-01 08:57:32
【问题描述】:

我的导航栏标题有问题。我有 2 个屏幕,当我单击屏幕 2 上的返回按钮时,它将返回屏幕 1,但屏幕 1 的标题消失了。这是我的 sn-p 代码

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";
      }
    

但标题仍然消失。请帮忙


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

- (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];
}

【问题讨论】:

  • 你是如何从 screen1 移动到 screen2 的?
  • 试试self.navigationItem.title
  • 第一次可见(在进入 VC2 之前)。你添加后退按钮。如果你使用导航栏,默认后退按钮将是 der
  • 您是否在导航控制器标题视图中添加了任何视图
  • @Suhail 是的,在移动到 screen2 之前它是可见的

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


【解决方案1】:

这解决了我的问题:

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

【讨论】:

  • 来自文档...If you override this method, you must call super in your implementation.
  • 这并没有解决我的问题。我必须添加调度队列。因此,如果有人需要添加代码 DispatchQueue.main.async { self.tabBarController?.navigationItem.title = "Your screen title" }
【解决方案2】:

就我而言,问题出在backItem?.title。在我的视图控制器和UINavigationController 的自定义子类中,我将其设置为空字符串:

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    ...
    navigationController.navigationBar.backItem?.title = ""
}

我的猜测是,当使用popViewController(animated:) 时,标题取自backItem?.title 并设置到主标题属性中。

【讨论】:

  • 这是旧的,但错误仍然存​​在,因为我不得不面对它...
【解决方案3】:

viewWillAppear 中实现这个:

self.navigationItem.title="Title text";

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

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

      override func viewWillAppear(animated: Bool) {
          super.viewWillAppear(animated)
      
          self.navigationController?.navigationBar.topItem?.title = ""
          self.navigationItem.title = "Title"
      }
      

      【讨论】:

        【解决方案6】:

        Objective c解决方案:

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

        【讨论】:

        • 来自文档...If you override this method, you must call super in your implementation.
        • @Fogmeister 完成 :)
        • @raed 应该是 [super viewWillAppear: animated];
        【解决方案7】:

        我来自 Swift 世界。但是我尝试了很多解决方案。这些都是动画造成的。如果您将 Bool 更改为 false,它将解决您的问题。最后,您可以将代码放入 viewWillAppear 函数中。

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

        【讨论】:

        • 来自文档...If you override this method, you must call super in your implementation.
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多