【问题标题】:Erratic dismissal of software keyboard软件键盘的不稳定解雇
【发布时间】:2018-03-13 12:10:25
【问题描述】:

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

【问题讨论】:

  • jeddi 遵循以下答案

标签: ios objective-c keyboard uialertview uialertviewcontroller


【解决方案1】:

我的答案是给你的

视图控制器

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

【讨论】:

    猜你喜欢
    • 2011-03-08
    • 1970-01-01
    • 2023-01-18
    • 2018-07-03
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    相关资源
    最近更新 更多