[英]UIBarButtonItem segue to next viewcontroller
我以編程方式創建了一個UIBarButtonItem,並希望它在按下時轉到下一個ViewController。
這是我在viewDidLoad中創建自定義UIBarButtonItem的方式:
UIBarButtonItem *foundButton = [[UIBarButtonItem alloc]
initWithTitle:@"Found it!"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(foundView)];
之后,我創建了此方法:
-(void)foundView:(id)sender {
UIViewController *foundVC = [self.storyboard instantiateViewControllerWithIdentifier:@"foundView"];
[self.navigationController pushViewController:foundVC animated:YES];
}
單擊UIBarButtonItem之后要轉到的ViewController具有一個Storyboard ID:“ foundView”。
我在這里做錯了什么?
像這樣更改代碼:
UIBarButtonItem *foundButton = [[UIBarButtonItem alloc]
initWithTitle:@"Found it!"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(foundView:)];
您忘記了:
在選擇器中的foundView
之后,因此您不會調用該方法。
您可以創建一個segue
從ViewControllerA
到ViewControllerB
。 注意,此segue
連接兩個視圖控制器。 不是視圖控制器的按鈕。 現在,在情節提要上,為segue賦予一個描述性名稱。 例如: segueFoundView
。
現在,執行以下操作:
-(void)foundView:(id)sender {
[self performSegueWithIdentifier:@"segueFoundView" sender:self];
}
編輯:糾正拼寫
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.