我想拦截 UITextField
上的触摸,例如,当我第一次加载它时,我给它一个 @selector()
来加载一个不同的方法,而不是delegate
东西?
这是我的尝试:
descriptionText = [[UITextField alloc] initWithFrame:CGRectMake(10.0, 25.0, infoView.frame.size.width - 20, 100.0)];
descriptionText.backgroundColor = [UIColor whiteColor];
descriptionText.textColor = [UIColor blackColor];
descriptionText.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
descriptionText.userInteractionEnabled = YES;
[descriptionText addTarget:self action:@selector(loadInfoView:) forControlEvents:UIControlEventTouchUpInside];
descriptionText.font = [UIFont fontWithName:@"Helvetica" size:15];
// show view
[infoView addSubview:descriptionText];
但是当我调试方法时:
- (void)loadInfoView
{
NSLog(@"load other view here");
}
最佳答案
您的问题出在forControlEvents:
:尝试UIControlEventEditingDidBegin
[descriptionText addTarget:self action:@selector(loadInfoView:) UIControlEventEditingDidBegin];
如果你有 - (void)loadInfoView
,你的 @selector(loadInfoView:)
也是错误的>
或@selector(loadInfoView:)
和- (void)loadInfoView: (id) sender
或 @selector(loadInfoView)
和 - (void)loadInfoView
但是,为什么不使用 UITextFieldDelegate
?
.m
@interface ViewController () <UITextFieldDelegate>
@property (strong, nonatomic) UITextField *descriptionText;
@end
切记:
self.descriptionText.delegate = self;
然后:
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
if (textField == self.descriptionText)
{
[self loadInfoView];
}
}
键盘:
如果你不想显示键盘你需要添加一个[descriptionText resignFirstResponder];
关于ios - 拦截 UITextField touch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22657743/