0

预期行为:用户在里面点击TextField1,键盘弹出,用户输入一个值,NextButton被按下,键盘应该被关闭。

异常:按 时键盘消失NextButton,但随后的警报消失后又弹出!为什么?

另一方面,如果从未调用警报(//[self showDisclaimer]),则键盘会正确关闭...

我知道它 alertView已被弃用,但这不是错误的根源,因为如果我使用它,我会得到完全相同的行为UIAlertController

有人可以对此有所了解吗?

- (IBAction) NextButton: (id) sender
{
    [self backgroundTouch:id];  //Dismisses the keyboard
    [self showDisclaimer]; 
}

- (void) showDisclaimer {

    UIAlertView *alertView = [[UIAlertView alloc] 
                 initWithTitle:@"Disclaimer" message: @"bla bla bla"                  
                 delegate:self 
                 cancelButtonTitle: nil
                 otherButtonTitles:@"Don't agree", @"I AGREE", nil];
    [alertView show];   
}


- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"I AGREE"])
    {
        [self showAlert];
    }
    else if([title isEqualToString:@"Don't agree"])
    {
        //Do something else 
    }
}

- (IBAction) backgroundTouch: (id)sender {

    [TextField1 resignFirstResponder];
}
4

1 回答 1

0

我的答案是给你的

视图控制器

。H

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelgate>  
@property (nonatomic, strong) IBOutlet UITextField *txtFld;
@property (nonatomic, strong) UITextField *currentTxtFld;
- (IBAction)NextButton:(id)sender;
@end

.m

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize currentTxtFld;
@synthesiz txtFld;


 - (void)viewDidLoad {
      txtFld.delegate = self; 
 }

 - (IBAction) NextButton: (id) sender
 {
     [currentTxtFld resignFirstResponder];
     [self showDisclaimer];             
 }

 - (void) showDisclaimer 
 {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Disclaimer" message:@"bla bla bla" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *agreeBtnAction = [UIAlertAction actionWithTitle:@"I AGREE" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
        .......//Your code HEre
    }];
    UIAlertAction *dontagreeBtnAction= [UIAlertAction actionWithTitle:@"Don't agree" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
        .......//Your code Here
    }];

    [alert addAction:agreeBtnAction];
    [alert addAction:dontagreeBtnAction];
    [self presentViewController:alert animated:YES completion:nil];

 }

 #pragma mark - UITextField Delegate methods

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
     currentTxtFld = textFld;
     return YES;
  }

 - (BOOL)textFieldShouldReturn:(UITextField *)textField {
     [textField resignFirstResponder];
     return YES;
  }    
于 2017-10-02T13:04:48.287 回答