我有UISearchBar,並且當我從UISearchBar單擊搜索按鈕時,我想顯示另一個UIViewController。我正在使用以下方法:由pushViewController調用的UIViewController出現的動畫顯示兩次
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main"
bundle:nil];
UIViewController *add =
[storyboard instantiateViewControllerWithIdentifier:@"mainMenuViewController"];
[self.navigationController pushViewController:add animated:YES];
}
問題是出現動畫的UIViewController被調用兩次。我看到舊的控制器從右到左滑動兩次。
現在,如果我使用相同的代碼來顯示新的控制器,當點擊一些常規的UIButton時,動畫問題就消失了。所以它似乎與UISearchBar有某種關係。
searchBarSearchButtonClicked的委託方法只被調用一次。
重現問題的代碼非常少,可能我會在稍後附上示例項目。
但隨着搜索欄的UIViewController看起來是這樣的:
- (void)viewDidLoad {
[super viewDidLoad];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
searchBar.placeholder = NSLocalizedString(@"search a pub or a place", nil);
searchBar.delegate = self;
[self.navigationItem setTitleView:searchBar];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
DLog(@"Filter searchBarSearchButtonClicked");
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main"
bundle:nil];
UIViewController *add =
[storyboard instantiateViewControllerWithIdentifier:@"mainMenuViewController"];
[self.navigationController pushViewController:add animated:YES];
}
問題既是在iOS 8和9
編輯:
我上傳示例項目here。這是非常簡約的。
EDIT2:
如果我提出這樣的控制器,而不是通過導航控制器,只顯示一次動畫。 (這是另一個動畫 - 從下往上,前一個從右到左)。但我需要通過導航控制器來完成。所以這個問題似乎與UISearchBar Search單擊+導航控制器有關。
[self presentViewController:add
animated:YES
completion:nil];
當然,我也可以把它叫做沒有動畫像這樣:
[self.navigationController pushViewController:add animated:NO];
但是如果我想要的動畫。
EDIT3:
我試圖通過SEGUE顯示第二控制器,但它並沒有幫助。
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self performSegueWithIdentifier:@"seg1" sender:self];
}
Edit4:
我已經報告這個錯誤給蘋果,因爲我想這UIKit中一定是錯誤。無論如何,如果有人會找到解決辦法,這將是件好事。
也許有些東西越來越混亂了UISearchController的解僱。有一件事是嘗試將按鈕委託代碼移動到一個帶有簡單簽名的方法中,然後用'self performSelector'調用它。 – danh
感謝您的想法,我已經嘗試過,但問題仍然存在。 – luky