2

我有三个 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
4

1 回答 1

6

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

//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:,如下所示。不要忘记UITextFieldDelegate在 .h 文件中添加:

在您的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;
}

这应该回答你的主要问题。对于附带的问题,这也很简单。您只需将 a 添加UIGestureRecognizer到您的视图中,调用一个将 firstResponder 重新分配给 selected 的方法UITextField。它会是这样的:

在某处设置手势识别器,例如在您的viewDidLoad方法中

- (void)viewDidLoad
{
    [super viewDidLoad];

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

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

- (void)dismissKeyboard {
   [self.view endEditing:YES];
}
于 2013-09-23T01:21:22.813 回答