0

Expected behaviour: user clicks inside TextField1, the keyboard pops up, user enters a value, NextButton is pressed, keyboard should be dismissed.

Anomaly: the keyboard gets dismissed upon pressing NextButton, however it then pops up again after the alerts that follow are dismissed! Why?

On the other hand, if the alert is never called (//[self showDisclaimer]) the keyboard gets dismissed correctly...

I know that alertView is deprecated, but this is not the source of the error, because I get exactly the same behaviour if I use UIAlertController instead.

Can someone shed some light on this?

- (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
  • 651
  • 1
  • 12
  • 21

1 Answers1

0

My answer is for you

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;
  }    
user3182143
  • 9,459
  • 3
  • 32
  • 39