UITextField 触摸事件在 UIScrollView 中不起作用
Posted
技术标签:
【中文标题】UITextField 触摸事件在 UIScrollView 中不起作用【英文标题】:UITextField touch events don`t work in UIScrollView 【发布时间】:2015-07-17 08:21:26 【问题描述】:我有一个 UITextField,当 UITextField 是 UIScrollView 的子视图时,文本字段操作不起作用。
其他方面,它工作正常, 这是我的代码.. 请帮忙?
我正在使用此代码,因为我需要在单击 alertview 按钮后立即触发 UIDatePicker。
- (void)viewDidLoad
[super viewDidLoad];
UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:scroll];
date = [[UITextField alloc]initWithFrame:CGRectMake(80, 150, 150, 50)];
date.placeholder = @"Date";
date.delegate = self;
[date addTarget:self action:@selector(dateClicked:) forControlEvents:UIControlEventTouchDown];
[scroll addSubview:date];
- (void)dateClicked:(id)sender
NSLog(@"CLICKED");
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Choose Date Format" message:nil delegate:self cancelButtonTitle:@"dd/MM/YYYY" otherButtonTitles:@"MM/dd/YYYY", nil];
[alertView show];
【问题讨论】:
使用 UIControlEventEditingDidBegin 代替 UIControlEventTouchUpInside 它的工作,但我想在更新文本字段文本的 alertView Button CLick 上打开一个 uipicker,如果我使用 UIControlEventEditingDidBegin 然后在更新文本后,alertview 将再次打开@Saurabh 【参考方案1】:设置UIButton
相同的帧UITextField
并设置UIButton
987654324 @事件和更新UITextField
值。 P>
- (void)viewDidLoad
[super viewDidLoad];
UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:scroll];
date = [[UITextField alloc]initWithFrame:CGRectMake(80, 150, 150, 50)];
date.placeholder = @"Date";
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(80, 150, 150, 50)];
[btn addTarget:self action:@selector(dateClicked:) forControlEvents:UIControlEventTouchUpInside];
[scroll addSubview:date];
[scroll addSubview:btn];
- (void)dateClicked:(id)sender
NSLog(@"CLICKED");
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Choose Date Format" message:nil delegate:self cancelButtonTitle:@"dd/MM/YYYY" otherButtonTitles:@"MM/dd/YYYY", nil];
[alertView show];
【讨论】:
它的工作,但我想在更新文本字段文本的 alertView Button CLick 上打开一个 uipicker,如果我使用 UIControlEventEditingDidBegin 然后在更新文本后,alertview 将再次打开@Dharmesh【参考方案2】:我根据您的要求创建了简单的示例,请看:
在.h文件中
UITextField *date;
NSMutableArray *arrData;
在 .m 文件中
- (void)viewDidLoad
[super viewDidLoad];
UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:scroll];
date = [[UITextField alloc]initWithFrame:CGRectMake(80, 150, 150, 50)];
date.placeholder = @"Date";
date.delegate = self;
[date addTarget:self action:@selector(dateClicked:) forControlEvents:UIControlEventEditingDidBegin];
[scroll addSubview:date];
arrData = [NSMutableArray array];
for(int i=0;i<10;i++)
[arrData addObject:[NSString stringWithFormat:@"Object - %d",i]]
- (void)dateClicked:(id)sender
NSLog(@"CLICKED");
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Choose Date Format" message:nil delegate:self cancelButtonTitle:@"dd/MM/YYYY" otherButtonTitles:@"MM/dd/YYYY", nil];
[alertView show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
UIPickerView *picker = [[UIPickerView alloc]initWithFrame:CGRectMake(80, 200, 150, 150)];
picker.delegate = self;
picker.dataSource = self;
[self.view addSubview:picker];
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
return 1;
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
return 5;
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
return arrData[row];
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
date.text = arrData[row];
[pickerView removeFromSuperview];
【讨论】:
以上是关于UITextField 触摸事件在 UIScrollView 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
UITextField 添加到 UIView 时不响应触摸事件