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

时间:2010-03-12 11:21:04

标签: iphone uibarbuttonitem

我不知道,为什么按钮在工具栏设置为隐藏和取消隐藏后消失。 我该如何解决?

设置按钮代码

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

3 个答案:

答案 0 :(得分:9)

用于设置工具栏项的documented method,通过视图控制器的toolbarItems属性same 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];

答案 1 :(得分:1)

看到没有更好的答案,我会宣传我之前的评论。试着拿出这一行:

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

我没有尝试过类似的东西,但它看起来不对,而且非常违反iPhone SDK理念。如果控制器对象已经有一个指向工具栏的指针,为什么还需要将它添加到视图中?如果这是适合它的地方,控制器对象就会自己做。

答案 2 :(得分:0)

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