ios - UITextField 触摸事件在 UIScrollView 中不起作用

标签 ios objective-c iphone uiscrollview uialertview

我有一个 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/

相关文章:

objective-c - NSTextFieldCell 垂直对齐,解决方案似乎压扁了水平对齐

ios - 检查不同语言的工作日字符串

ios - 使用 Xcode 7.2 录制时 XCUITest 崩溃

iphone - 是否需要在 alloc 语句中设置 autorelease?

ios - 我需要将一些文件从现有的git repo移到新文件。我可以保留他们的历史吗?

ios - 在 GET 请求中传递 JSON 对象 : iOS

ios - 页面滚动在启用自动布局的情况下不起作用

ios - 为 XCTest 模拟较低的操作系统版本

ios - 关闭 Presentation ViewController

iphone - 将大量数据写入 NSData 对象-iOS