問題描述
使用 iOS 5 故事板,在一個按鈕上我正在執行 segue,我想要對我的文本字段進行驗證,如果驗證失敗,我必須停止 segue 并發出警報.有什么辦法嗎?
Using iOS 5 storyboards, on a button I am performing a segue, what I want is to do validation on my textfield and if validation is failed I have to stop segue and throw an alert. Whats the way to do it?
推薦答案
如果您的部署目標是 iOS 6.0 或更高版本
您可以在源視圖控制器上簡單地實現 shouldPerformSegueWithIdentifier:sender:
方法.如果你想執行segue,讓這個方法返回YES
,如果你不想,則返回NO
.
If your deployment target is iOS 6.0 or later
You can simply implement the shouldPerformSegueWithIdentifier:sender:
method on your source view controller. Make this method return YES
if you want to perform the segue, or NO
if you don't.
您需要更改故事板中的 segue 連接方式并編寫更多代碼.
You will need to change the way your segue is connected in the storyboard and write a little more code.
首先,設置從按鈕的視圖控制器到目標視圖控制器的 segue,而不是直接從按鈕到目標.給 segue 一個標識符,如 ValidationSucceeded
.
First, set up the segue from the button's view controller to the destination view controller, instead of directly from the button to the destination. Give the segue an identifier like ValidationSucceeded
.
然后,將按鈕連接到其視圖控制器上的操作.在操作中,執行驗證并根據驗證是否成功執行 segue 或顯示警報.它看起來像這樣:
Then, connect the button to an action on its view controller. In the action, perform the validation and either perform the segue or show an alert based on whether the validation succeeded. It will look something like this:
- (IBAction)performSegueIfValid:(id)sender {
if ([self validationIsSuccessful]) {
[self performSegueWithIdentifier:@"ValidationSucceeded" sender:self];
} else {
[self showAlertForValidationFailure];
}
}
這篇關于停止 segue 并顯示警報的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!