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

Iphone 隐藏和取消隐藏工具栏时按钮项丢失,iphone,uibarbuttonitem,Iphone,Uibarbuttonitem,我不知道,为什么按钮消失后,工具栏设置为隐藏和取消隐藏。 我怎样才能修好它 设置按钮代码 -(void)viewDidAppear:(BOOL)animated { //NSLog(@"viewDidAppear "); [self becomeFirstResponder]; //Create a button UIBarButtonItem *back = [[UIBarButtonItem alloc] i

我不知道,为什么按钮消失后,工具栏设置为隐藏和取消隐藏。 我怎样才能修好它

设置按钮代码

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

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

中释放它们。如果没有更好的答案,我将推广我之前的评论。试着去掉这一行:

[self.navigationController.view addSubview:self.navigationController.toolbar];
我还没有尝试过类似的东西,但它看起来是错误的,而且与iphonesdk的理念背道而驰。如果控制器对象已经有指向工具栏的指针,为什么需要将其添加到视图中?如果这是正确的位置,控制器对象将自己执行此操作。

通过视图控制器的toolbarItems属性设置工具栏项。还将
工具栏
属性列为只读,并特别警告

不应修改UIToolbar 直接反对

因此,尝试改变

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


你能把你的代码贴在显示按钮的地方吗?这是您的ViewDidDisplay方法此行的目的是什么:[self.navigationController.view addSubview:self.navigationController.toolbar];它要么是多余的,要么是完全错误的。文档中说工具栏是“在展示行动表时使用的”,我不相信你会这么做。即使这样,我也不明白为什么需要将它添加到视图中。好的,我删除它!同意你的看法那部分没什么问题。导航控制器保留对它们的引用,这就足够了。谢谢语法,你说得对,但是当我隐藏和取消隐藏工具栏时,我的按钮消失了。我如何才能调用它们?请向我们显示你用于隐藏和取消隐藏的代码。此外,如果工具栏位于导航栏中,则可能不必同时隐藏两者——只需隐藏导航栏即可。此代码用于隐藏工具栏[self.navigationController setNavigationBarHidden:YES动画:YES];[[UIApplication sharedApplication]setStatusBarHidden:是动画:是];[self.navigationController设置工具栏隐藏:是动画:是];。。。把它藏起来?这就是我真正需要看到的一个。如果(切换){[self.navigationController setNavigationBarHidden:YES动画:YES];[UIApplication sharedApplication]setStatusBarHidden:YES动画:YES];[self.navigationController setToolbarHidden:YES动画:YES];self.navigationController.hidesBottomBarWhenPushed=YES;}{[[UIApplication sharedApplication]setStatusBarHidden:否动画:是];[self.navigationController setNavigationBarHidden:否动画:是];[self.navigationController setToolbarHidden:否动画:是];}谢谢David Gelhar它工作正常,我的按钮隐藏后又回来了!回答很好…为我节省了20分钟我确定:)
[self setToolbarItems:[NSArray arrayWithObjects:fixspace1, back, fixspace1, stop, fixspace1, next, fixspace1, nil] animated:YES];