我有一个 UITextField,当 UITextField 是 UIScrollView 的 subview 时,其中文本字段操作不起作用。
除此之外,一切正常, 这是我的代码..请帮忙?
我正在使用这段代码,因为我需要在单击 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];
}
最佳答案
您将 UIButton
设置为与 UITextField
相同的框架并设置 UIButton
UIControlEventTouchUpInside
事件并更新 UITextField
值。
- (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];
}
关于ios - UITextField 触摸事件在 UIScrollView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471310/