Ошибочное увольнение программной клавиатуры
Ожидаемое поведение: пользователь нажимает внутри 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 ответ
Мой ответ для тебя
ViewController
.час
#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;
}