下一个按钮将焦点带到下一个UITextField?

时间:2013-09-23 00:47:47

标签: ios objective-c uitextfield

我有三个UITextFields,我需要第一个选项卡到第二个时候点击下一个,第二个选项卡到第三个时候点击下一个。最后是第三个隐藏键盘。作为一个侧面问题,用户可以通过点击所有文本字段上屏幕上的任何其他位置来隐藏键盘吗?

这是我的代码:

#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

1 个答案:

答案 0 :(得分:6)

这很简单。首先,以升序方式设置带有标记的textFields,如下所示。而且,将它的代表设置为自我:

//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;

然后,实现UITextField委托方法textFieldShouldReturn:,如下所示。不要忘记在.h文件中添加UITextFieldDelegate

ViewController.h文件中:

@interface ViewController : UIViewController <UITextFieldDelegate>

ViewController.m文件中:

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

这应该回答你的主要问题。对于附带问题,这也很简单。您只需在视图中添加UIGestureRecognizer,调用将firstResponder转移到所选UITextField的方法即可。它将是这样的:

在某处设置手势识别器,就像在viewDidLoad方法

中一样
- (void)viewDidLoad
{
    [super viewDidLoad];

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

并实现执行dismiss的方法,如下所示:

- (void)dismissKeyboard {
   [self.view endEditing:YES];
}