工具栏隐藏和显示时按钮项丢失

3

我不知道为什么在隐藏和显示工具栏后按钮会消失。如何修复它?

设置一个按钮代码。

-(void)viewDidAppear:(BOOL)animated {
    //NSLog(@"viewDidAppear ");

    [self becomeFirstResponder];
    //Create a button
    UIBarButtonItem *back = [[UIBarButtonItem alloc] 
                        initWithBarButtonSystemItem:UIBarButtonSystemItemRewind 
                target:self action:@selector(goback:)];

    UIBarButtonItem *fixspace1 = [[UIBarButtonItem alloc] 
                                 initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                 target:self action:nil];

    UIBarButtonItem *next = [[UIBarButtonItem alloc] 
                             initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward 
                             target:self action:@selector(gofwd:)];
    UIBarButtonItem *stop = [[UIBarButtonItem alloc] 
                             initWithBarButtonSystemItem:UIBarButtonSystemItemStop 
                             target:self action:@selector(stopload:)];

    UIBarButtonItem *refresh = [[UIBarButtonItem alloc] 
                             initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh 
                             target:self action:@selector(refreshWeb:)];


    [self.navigationController.toolbar setItems:[NSArray arrayWithObjects:fixspace1, back, fixspace1, stop, fixspace1, next, fixspace1, nil] animated:YES];
    [self.navigationItem setRightBarButtonItem:refresh animated:YES];

    [self.navigationController.view addSubview:self.navigationController.toolbar];

    [stop release];
    [next release];
    [back release];
    [refresh release];
    [fixspace1 release];
}

我在这个方法中设置了我的按钮

-(void)viewDidAppear:(BOOL)animated 

此代码用于隐藏工具栏

    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
    [self.navigationController setToolbarHidden:YES animated:YES];

alt text


你能否发布一下你展示按钮的代码吗?那应该是你的viewDidAppear方法。 - pheelicks
这行代码的目的是什么: [self.navigationController.view addSubview:self.navigationController.toolbar]; 它似乎要么是多余的,要么就是完全错误的。文档说工具栏是“用于显示操作表”,但我不认为你正在这样做。即使这样,我也看不出为什么需要将其添加到视图中。 - Amagrammer
好的,我移除它!同意你的意见。 - RAGOpoR
3个回答

9
文档中所述的方法通过视图控制器的toolbarItems属性来设置工具栏项目。同样UINavigationController参考文档也将toolbar属性列为只读,并特别警告:

不应直接修改UIToolbar对象。

因此,请尝试更改:
[self.navigationController.toolbar setItems:[NSArray arrayWithObjects:fixspace1, back, fixspace1, stop, fixspace1, next, fixspace1, nil] animated:YES];

to

[self setToolbarItems:[NSArray arrayWithObjects:fixspace1, back, fixspace1, stop, fixspace1, next, fixspace1, nil] animated:YES];

感谢David Gelhar,它起作用了,我的按钮在隐藏后又回来了! - RAGOpoR
很棒的答案...一定节省了我20分钟的时间 :) - jkp

1

没有更好的答案,我将提升我之前的评论。试试把这行删除:

[self.navigationController.view addSubview:self.navigationController.toolbar];

我没有尝试过类似的东西,但它看起来是错误的,并且非常违反iPhone SDK的哲学。如果控制器对象已经有了指向工具栏的指针,为什么还需要将其添加到视图中呢?如果那是正确的位置,控制器对象会自己完成这个任务。


感谢Amagrammer的指正,你说得没错。但是当我隐藏和显示工具栏时,我的按钮消失了,我该怎么调用它们? - RAGOpoR
请展示您用于隐藏和取消隐藏的代码。此外,如果工具栏位于导航栏中,则可能只需隐藏导航栏,而无需同时隐藏两者。 - Amagrammer
这段代码用于隐藏工具栏:[self.navigationController setNavigationBarHidden:YES animated:YES]; [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES]; [self.navigationController setToolbarHidden:YES animated:YES]; - RAGOpoR
...并取消隐藏?那是我真正需要看到的。 - Amagrammer
如果(切换){ [self.navigationController setNavigationBarHidden:YES animated:YES]; [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES]; [self.navigationController setToolbarHidden:YES animated:YES]; self.navigationController.hidesBottomBarWhenPushed = YES; } 否则 { [[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES]; [self.navigationController setNavigationBarHidden:NO animated:YES]; [self.navigationController setToolbarHidden:NO animated:YES]; } - RAGOpoR
我会考虑移除工具栏的隐藏和取消隐藏功能。如果它附加在导航栏上,那么它应该会随着导航栏的显示和隐藏而自动处理。不过还不确定。 - Amagrammer

0

我认为你不应该在将工具栏按钮添加到工具栏后立即释放它们。你应该将它们保存在实例变量中,并在dealloc中释放。


那部分没有问题。导航控制器保留对它们的引用,这就足够了。 - Amagrammer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接