2

I have three UITextFields and I need the first to tab to the second when next is clicked and the second to tab to the third when next is clicked. And finally the third to hide the keyboard. And as a side question can the user hide the keyboard by clicking anywhere else on the screen on all of the text fields?

Here is my code:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


-(void) procrastinationNotificationSwitchOnOrOff {

    if (_procrastinationNotificationSwitch.on) {
        _notificationOnOffLabel.text = @"Procrastination Notification On";
        self.notificationStatus = @"NOTIFICATION ON";
        NSLog(self.notificationStatus);
    }
    else {
        _notificationOnOffLabel.text = @"Procrastination Notification Off";
        self.notificationStatus = @"NOTIFICATION OFF";
        NSLog(self.notificationStatus);
    }
}


-(void) presentMessage:(NSString *)message {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Class Stuff" message:message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];

}

-(void) notificationStatus:(NSString *)stat {
   NSString *status = [NSString stringWithFormat:@"%@", stat];

}

-(IBAction)addButton:(id)sender {


    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
    dateFormatter.timeStyle = NSDateFormatterShortStyle;
    dateFormatter.dateStyle = NSDateFormatterShortStyle;

    NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date];
    NSLog(@"Alarm Set Button Tapped : %@", dateTimeString );
    NSString *classNameString = self.className.text;
    NSLog(classNameString);
    NSString *assignmentTitleString = self.assignmentTitle.text;
    NSLog(assignmentTitleString);
    NSString *assignmentDescriptionString = self.assignmentDescription.text;
    NSLog(assignmentDescriptionString);
    NSString *totalStrings = [NSString stringWithFormat:@"Class Name: %@\r Assignment Title: %@ \rAssignment Description: %@ \rDue: %@ \r%@", classNameString, assignmentTitleString, assignmentDescriptionString, dateTimeString, self.notificationStatus];
    NSLog(totalStrings);


    [self presentMessage:totalStrings];



}


-(IBAction)returnKeyButton:(id)sender {
    [sender resignFirstResponder];

    }


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [_procrastinationNotificationSwitch addTarget:self action:@selector(procrastinationNotificationSwitchOnOrOff) forControlEvents:UIControlEventValueChanged];
    self.notificationStatus = @"NOTIFICATION OFF";
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
Sebyddd
  • 4,305
  • 2
  • 39
  • 43
AbdelElrafa
  • 881
  • 8
  • 16

1 Answers1

6

That's pretty simple. First of all, set your textFields with tag in a ascending way, like this. And also, set it's delegates to self:

//set up this somewhere
self.nameTextField.tag = 0;
self.nameTextField.delegate = self;

self.emailTextField.tag = 1;
self.emailTextField.delegate = self;

self.passwordTextField.tag = 2;
self.passwordTextField.delegate = self;

And then, implement the UITextField delegate's method textFieldShouldReturn: like below. Don't forget to add the UITextFieldDelegate in your .h file:

In your ViewController.h file:

@interface ViewController : UIViewController <UITextFieldDelegate>

And in your ViewController.m file:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{      
    NSUInteger index = textField.tag;

    if (index == 2) { // Last textField
        [textField resignFirstResponder];
    }else{

        UITextField *nextTextField = (UITextField*)[self.view viewWithTag:index+1];
        [nextTextField becomeFirstResponder];

    }
    return NO;
}

This should answer your main question. And for the side question, that's simple too. You just have to add a UIGestureRecognizer to your view, calling a method that resigns the firstResponder to the selected UITextField. It will be something like this:

Set-up the gesture recognizer somewhere, like in your viewDidLoad method

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITapGestureRecognizer *dismissKeyboard = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:dismissKeyboard];
}

And implement the method that performs the dismiss, like this:

- (void)dismissKeyboard {
   [self.view endEditing:YES];
}
Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49
  • When you say set this up somewhere, where do you mean. I am a beginner. – AbdelElrafa Sep 23 '13 at 01:25
  • It can be in your `viewDidLoad` method, of your `UIViewController`. I edited the asnwer, detailing a little bit more – Lucas Eduardo Sep 23 '13 at 01:27
  • it says that the dismissKeyboard is unused even though I did it exactly as you said? – AbdelElrafa Sep 23 '13 at 01:56
  • Oh, I forgot the code, but you have to add the gesture recognizer in your view. I edited, now should work and get rid of the warning. – Lucas Eduardo Sep 23 '13 at 02:03
  • I got it thanks to:http://stackoverflow.com/questions/5306240/iphone-dismiss-keyboard-when-touching-outside-of-textfield – AbdelElrafa Sep 23 '13 at 02:14
  • all good, but dissmissKeyboard. This can be shorten: [self.view endEditing:NO]; – bucherland Apr 29 '14 at 05:07
  • @bucherland I thing you meant `[self.view endEditing:YES];` right? Updated – Lucas Eduardo May 05 '14 at 01:28
  • I meant `NO`. `YES` will go for forced `endEditing` as it've been said in documentation. So I believe the difference between `YES` and `NO` is that when `NO` the view will ask UITextField delegates for `textFieldShouldEndEditing:` if they allow to do so. – bucherland May 05 '14 at 13:06