我有一个文本字段和一个按钮。按下按钮时,它会调用例程。我希望退出时的textfield结束调用相同的例程而不必复制代码。 ViewController.h位于
之下 #import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityind;
@property (weak, nonatomic) IBOutlet UITextField *search;
- (IBAction)calculate:(id)sender;
@end
答案 0 :(得分:0)
在ViewController.h中实现UITextFieldDelegate,如下所示:
@interface ViewController : UIViewController <UITextFieldDelegate>
然后使用方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
并调用您的IBAction和resignFirstResponder,我也会自动启用返回键。
答案 1 :(得分:0)
<强> [EDIT1] 强>
在viewController标题中:
@interface ViewController : UIViewController <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityind;
@property (weak, nonatomic) IBOutlet UITextField *search;
-(void)doSomeWork;
-(IBAction)calculate:(id)sender;
@end
只需在第二层例程中实现该功能。
在viewController.m文件中:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
if(search == textField){
[textField resignFirstResponder];
[self doSomeWork];
}
return YES;
}
-(void)doSomeWork{
// Do whatever you want to do here!!!
}
-(IBAction)calculate:(id)sender{
[self doSomeWork];
}
在xib文件中,您必须将按钮连接到“计算”操作,搜索到正确的UITextField。
您可以在Interface Builder中或代码中以图形方式设置UITextField的委托。如果在代码中,则在viewController.m文件中添加以下行:
search.delegate = self;
到viewDidLoad方法,如下所示:
-(void)viewDidLoad{
[super viewDidLoad];
search.delegate = self;
}