ios - 拦截 UITextField touch

标签 ios objective-c uitextfield uitextfielddelegate uicontrolevents

我想拦截 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/

相关文章:

ios - Unity3d iOS操纵杆?

ios - Storyboard uiviewcontroller, 'custom class' 未显示在下拉列表中

ios - 使用多个自定义 UITableViewCells

ios - 带有文本输入到 UITextField 的自定义键盘

ios - 如何在 Swift 中将 UITextField 文本居中

ios - 无法在 Swift 中导入 Obj-C 框架

ios - 单点触摸错误: mtouch exited with code 1

objective-c - USB 设备发送/接收数据

objective-c - 如何在IOS中写入文件

ios - becomeFirstResponder 非常慢