[英]Erratic dismissal of software keyboard
預期的行為:用戶在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];
}
我的答案是給你的
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.