软件键盘

时间:2017-10-02 12:11:52

标签: ios objective-c keyboard uialertview uialertviewcontroller

预期行为: 用户在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];
}

1 个答案:

答案 0 :(得分:0)

我的回答是给你的

的ViewController

·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;
  }