[英]button item lost when hide and unhide toolbar
我不知道,為什么按鈕在工具欄設置為隱藏和取消隱藏后消失。 我該怎么辦呢?
設置按鈕代碼
-(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];
用於設置工具欄項的文檔化方法是通過視圖控制器的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];
看到沒有更好的答案,我會宣傳我之前的評論。 嘗試取出這一行:
[self.navigationController.view addSubview:self.navigationController.toolbar];
我沒有嘗試過類似的東西,但它看起來不對,而且非常違反iPhone SDK的理念。 如果控制器對象已經有一個指向工具欄的指針,為什么還需要將它添加到視圖中? 如果那是適合它的地方,控制器對象就會自己做。
我懷疑你是否應該在將它們添加到工具欄后立即釋放它們。 您應該將它們保存在實例變量中並在dealloc
釋放它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.