2012-01-12 68 views
0

我的应用程序中创建了许多文本框,并且我希望它们在用户完成编辑时关闭键盘。我知道,如果文本框的通过IB创建的,并且有一个IBAction为我可以使用,但如果这样创建文本字段的内容,以编程方式触发didEndOnExit

int top = 60; 
for(NSUInteger i=0; i< [kArray count]; i++){ 
     UITextField *kField = [kArray objectAtIndex:i]; 
     kField.frame = CGRectMake(130, top, 60, 30); 
     kField.borderStyle = UITextBorderStyleRoundedRect; 
     kField.autocorrectionType = UITextAutocorrectionTypeNo; 
     kField.returnKeyType = UIReturnKeyDone; 
     kField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; 

     [scrollView addSubview:kField]; 
     top += 50; 
    } 

我怎么能收当用户按下完成按钮键盘?

回答

2

使用UITextFieldDelegate方法:

#pragma mark UITextFieldDelegate methods 

- (BOOL)textFieldShouldReturn:(UITextField *)textField{ 
    [textField resignFirstResponder]; 
    return YES; 
} 

记得让你的类符合委托像这样:

@interface MyViewController : UIViewController <UITextFieldDelegate> 
+0

不要忘记分配您的看法控制器作为'kField'的代表。 'kField.delegate = self'。 – 2012-01-12 21:38:25

0

试试这个:

- (BOOL)textFieldShouldReturn: (UITextField *)textField { 
    [kField resignFirstResponder]; 
    return YES; 
} 
相关问题