39

iOS 5ストーリーボードを使用して、セグエを実行しているボタンで、テキストフィールドで検証を実行します。検証が失敗した場合は、セグエを停止してアラートをスローする必要があります。それを行う方法は何ですか?

4

3 に答える 3

77

展開ターゲットが iOS 6.0 以降の場合

shouldPerformSegueWithIdentifier:sender:ソースビューコントローラーにメソッドを実装するだけです。YESセグエを実行する場合、または実行しない場合は、このメソッドを返してくださいNO

展開ターゲットが iOS 6.0 より前の場合

ストーリーボードでのセグエの接続方法を変更し、もう少しコードを記述する必要があります。

最初に、ボタンから宛先へ直接ではなく、ボタンのView Controllerから宛先View Controllerへのセグエを設定します。セグエに のような識別子を付けValidationSucceededます。

次に、ボタンをビュー コントローラーのアクションに接続します。アクションでは、検証を実行し、検証が成功したかどうかに基づいてセグエを実行するか、アラートを表示します。次のようになります。

- (IBAction)performSegueIfValid:(id)sender {
    if ([self validationIsSuccessful]) {
        [self performSegueWithIdentifier:@"ValidationSucceeded" sender:self];
    } else {
        [self showAlertForValidationFailure];
    }
}
于 2012-02-23T06:09:57.523 に答える
38

私にとってうまくいったことと、正しい答えであると私が信じていることは、Apple Developer Guideにある UIViewController メソッドを使用することです:

shouldPerformSegueWithIdentifier:sender:

私は自分の方法を次のように実装しました:

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    if ([identifier isEqualToString:@"Identifier Of Segue Under Scrutiny"]) {
        // perform your computation to determine whether segue should occur

        BOOL segueShouldOccur = YES|NO; // you determine this
        if (!segueShouldOccur) {
            UIAlertView *notPermitted = [[UIAlertView alloc] 
                                initWithTitle:@"Alert" 
                                message:@"Segue not permitted (better message here)" 
                                delegate:nil 
                                cancelButtonTitle:@"OK" 
                                otherButtonTitles:nil];

            // shows alert to user
            [notPermitted show];

            // prevent segue from occurring 
            return NO;
        }
    }

    // by default perform the segue transition
    return YES;
}

魔法のように働きました!


>= iOS 8 の Swift で更新:

override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool {
    if identifier == "Identifier Of Segue Under Scrutiny" {
        // perform your computation to determine whether segue should occur

        let segueShouldOccur = true || false // you determine this
        if !segueShouldOccur {
            let notPermitted = UIAlertView(title: "Alert", message: "Segue not permitted (better message here)", delegate: nil, cancelButtonTitle: "OK")

            // shows alert to user
            notPermitted.show()

             // prevent segue from occurring
            return false
        }
    }

    // by default perform the segue transitio
    return true
}
于 2012-09-28T06:27:36.837 に答える
0

例を挙げます。これが私のコードです。

- (IBAction)Authentificate:(id)sender {
if([self WSAuthentification]){
   [self performSegueWithIdentifier:@"authentificationSegue" sender:sender];
}
else
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Authetification Failed" message:@"Please check your Identifications" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
    [alert show];
}

しかし、それは機能していないようです。すべての場合、私のセグエが実行されます。答えは簡単です。ボタンからではなく、View Controller 自体からセグエを配線する必要があります。

于 2013-06-20T14:53:41.220 に答える