2013-09-23 81 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

相關問題