2013-09-23 56 views
2

我有三个UITextFields,我需要第一个标签到第二个时候点击下一个,第二个标签到第三个时候点击下一个。最后第三个隐藏键盘。作为一个侧面的问题,用户可以通过点击所有文本字段上的任何其他位置来隐藏键盘?下一步按钮将焦点带到下一个UITextField?

这里是我的代码:

#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 

回答

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:像下面。不要忘记添加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; 
} 

这应该回答你的主要问题。对于侧面问题,这也很简单。您只需将UIGestureRecognizer添加到您的视图中,即可调用将firstResponder重新设置为所选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]; 
} 
+0

当你说把它放在某个地方,你的意思是什么。我是一名初学者。 – AbdelElrafa

+0

它可以在你的'UIViewController'的'viewDidLoad'方法中。我编辑了asnwer,详细说明了一点点 –

+0

它说dismissKeyboard是未使用的,即使我完全按照你所说的去做了吗? – AbdelElrafa

相关问题