隐藏和取消隐藏工具栏时按钮项丢失

发布于 2024-08-25 01:58:36 字数 2124 浏览 7 评论 0原文

我不知道,为什么工具栏设置为隐藏和取消隐藏后按钮消失了。 我该如何修复它?

设置按钮代码

-(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

i dont know, why the button are disappear after the toolbar set to hide and unhide.
how can i fix it?

setup a button code

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

and i setup my button at this method

-(void)viewDidAppear:(BOOL)animated 

this code use for hide toolbar

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

alt text

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

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

发布评论

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

评论(3

尘曦 2024-09-01 01:58:36

通过视图控制器的toolbarItems 属性相同的 UINavigationController Reference 还将 toolbar 属性列为只读并特别警告

您不应该修改 UIToolbar
直接反对。

因此,尝试更改

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

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

The documented method for setting toolbar items is via the toolbarItems property of the view controller. The same UINavigationController Reference also lists the toolbar property as read-only and specifically warns

You should not modify the UIToolbar
object directly.

Therefore, try changing

[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];
半葬歌 2024-09-01 01:58:36

看到没有更好的答案,我将推广我之前的评论。尝试去掉这一行:

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

我还没有尝试过类似的东西,但它看起来是错误的,并且非常违背 iPhone SDK 的理念。如果控制器对象已经有一个指向工具栏的指针,为什么需要将其添加到视图中?如果那是正确的位置,控制器对象就会自行执行此操作。

Seeing no better answers, I'll promote my earlier comment. Try taking out this line:

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

I haven't experimented with anything like that but it looks wrong and very much against the iPhone SDK philosophy. If the controller object already has a pointer to the toolbar, why would you need to add it to the view? If that's the right place for it, the controller object would do that itself.

毁我热情 2024-09-01 01:58:36

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

I doub't that you should release your toolbar buttons immediately after adding them to the toolbar. You should save them in instance variables and release them in your dealloc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文