“[setToolBarHidden:YES]”不工作

发布于 2024-12-12 12:44:23 字数 2051 浏览 2 评论 0原文

我有一个 UINavigationController,在它的视图控制器之一中,我使工具栏不隐藏在 viewDidAppear 中。效果很好。但是,在 viewDidDisappear 中,我将其设置为隐藏,但它并没有隐藏。我做错了什么?这是相关代码:

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

    [[self navigationController] setToolbarHidden:NO];

    UIBarButtonItem *buttomSubmit = [[UIBarButtonItem alloc] initWithTitle:@"Submit" 
                                                                   style:UIBarButtonItemStyleBordered 
                                                                  target:self 
                                                                  action:@selector(done)];
    UIBarButtonItem *buttonPrint = [[UIBarButtonItem alloc] initWithTitle:@"Print" 
                                                                     style:UIBarButtonItemStyleBordered 
                                                                    target:self 
                                                                    action:@selector(done)];
    UIBarButtonItem *buttonUnits = [[UIBarButtonItem alloc] initWithTitle:@"Units" 
                                                                    style:UIBarButtonItemStyleBordered 
                                                                   target:self 
                                                                   action:@selector(done)];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                               target:nil 
                                                                               action:nil];

    [self setToolbarItems:[NSArray arrayWithObjects:buttonUnits, flexSpace, buttomSubmit, buttonPrint, nil]];

    [buttomSubmit release];
    [buttonPrint release];
    [buttonUnits release];
    [flexSpace release];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[self navigationController] setToolbarHidden:YES];
}

谢谢!

I have a UINavigationController and in one of it's view controller, I am making the toolbar not hidden in viewDidAppear. Works just fine. But, in viewDidDisappear, I am setting it as hidden but it does not get hidden. What am I doing wrong? Here's the relevant code:

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

    [[self navigationController] setToolbarHidden:NO];

    UIBarButtonItem *buttomSubmit = [[UIBarButtonItem alloc] initWithTitle:@"Submit" 
                                                                   style:UIBarButtonItemStyleBordered 
                                                                  target:self 
                                                                  action:@selector(done)];
    UIBarButtonItem *buttonPrint = [[UIBarButtonItem alloc] initWithTitle:@"Print" 
                                                                     style:UIBarButtonItemStyleBordered 
                                                                    target:self 
                                                                    action:@selector(done)];
    UIBarButtonItem *buttonUnits = [[UIBarButtonItem alloc] initWithTitle:@"Units" 
                                                                    style:UIBarButtonItemStyleBordered 
                                                                   target:self 
                                                                   action:@selector(done)];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                               target:nil 
                                                                               action:nil];

    [self setToolbarItems:[NSArray arrayWithObjects:buttonUnits, flexSpace, buttomSubmit, buttonPrint, nil]];

    [buttomSubmit release];
    [buttonPrint release];
    [buttonUnits release];
    [flexSpace release];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[self navigationController] setToolbarHidden:YES];
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是我的挚爱i 2024-12-19 12:44:23

viewDidDisappear 在视图离开屏幕后调用。如果视图由于按下后退按钮而离开屏幕,它将被从导航控制器堆栈中弹出。

来自 UIViewController 类参考 注释在 navigationController 属性上:

仅当视图控制器位于其内部时才返回导航控制器
堆。如果无法导航控制器,则此属性为零
找到了。

这意味着 [self navigationController] 返回 nil,因此 setToolbarHidden 消息被发送到 nil 并且不起作用。

要在新视图加载后隐藏它(这似乎是您想要的),您可以在新视图控制器的 viewDidAppear 方法中进行隐藏。

viewDidDisappear is called after the view has gone off screen. If the view has gone off screen because of pressing the back button it will have been popped off the navigation controller stack.

From the UIViewController class reference notes on the navigationController property:

Only returns a navigation controller if the view controller is in its
stack. This property is nil if a navigation controller cannot be
found.

This means that [self navigationController] is returning nil and therefore the setToolbarHidden message is sent to nil and has no effect.

To hide it after the new view loads which is what you seem to want you could do the hiding in the viewDidAppear method of the new view's controller instead.

递刀给你 2024-12-19 12:44:23

在视图离开屏幕之前隐藏它是可以的。

-(void)viewWillDisappear:(BOOL)animated{
     [super viewWillDisappear:animated];
     [self.navigationController setToolbarHidden:YES animated:YES];
}

It's okay to hide it before the view goes off screen.

-(void)viewWillDisappear:(BOOL)animated{
     [super viewWillDisappear:animated];
     [self.navigationController setToolbarHidden:YES animated:YES];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文