1

私は作成しましたUIAlertView

alert = [[UIAlertView alloc] initWithTitle:@"Test"
                                   message:@"Working"
                                  delegate:self
                         cancelButtonTitle:@"Ok"
                         otherButtonTitles:nil];

[alert setAlertViewStyle:UIAlertViewStyleSecureTextInput]; 
alert.tag = kAlertTypePIN;

UITextField *textField = [alert textFieldAtIndex:0];
textField.delegate = self;

テキストフィールドでRetunキーを押すと、正常UIAlertViewに動作し、次のように呼び出されます:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
   [alert dismissWithClickedButtonIndex:0 animated:YES];
}

その後

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
  NSLog(@"didDismissWithButtonIndex");
  // If no text or wrong text show alert again otherwise normal actions
}

しかし、キャンセルボタンを押すと、最初に呼び出さtextFieldDidEndEditingれ、アラートデリゲートが呼び出されます。また、アラート デリゲート メソッドを単独で呼び出します。

そのため、表示されるアラートが表示されず、キーボードがポップアップして元に戻ります。したがって、アラートが表示される場合に備えて、アラートは表示されません。

流れに疑問があれば質問してください。

問題を修正するにはどうすればよいですか?

4

5 に答える 5

1

の textField のデリゲートを設定解除しますalertView:willDismissWithButtonIndex:

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
    UITextField *textField = [alert textFieldAtIndex:0];
    textField.delegate = nil;
}

アラートが閉じられると、textField は編集を終了し、その後textFieldDidEndEditing:デリゲート メソッドを呼び出します。
解任が開始される前にデリゲートを nil に設定すると、デリゲート メソッドを呼び出すことができません。

それに加えて、キャンセル ボタン"Cancel"と、別のボタン"Submit"を用意することをお勧めします。終了したら、「キャンセル」ではなく「送信」textFieldでアラートを閉じます。

于 2013-05-17T07:43:33.800 に答える
0

「キャンセル」ボタンのクリック時に UIAlertiView を非表示にする場合は、次のコードを clickedButtonAtIndex に追加するだけです。

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
     if(buttonIndex == 1)
     {
          [[alert textFieldAtIndex:0] resignFirstResponder];
     } 
}
于 2013-05-17T10:26:34.453 に答える
0

UIAlertiViewキャンセルクリックで非表示にしたいだけですか?

次に、キャンセルbuttonindexが 1 の場合:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

    if(buttonIndex == 1) {
        [[alert textFieldAtIndex:0] resignFirstResponder];
    }
}
于 2013-05-17T07:31:33.480 に答える
0

この条件を試してください..ピンがないときにアラートが消えないようにしたいので...

- (void)textFieldDidEndEditing:(UITextField *)textField
    {
       if([textField.text length]!=0)
         [alert dismissWithClickedButtonIndex:0 animated:YES];
    }
于 2013-05-17T07:32:25.303 に答える