gpt4 book ai didi

ios - 拦截 UITextField touch

转载 作者:行者123 更新时间:2023-11-28 22:12:05 28 4
gpt4 key购买 nike

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com