2

我对 iOS 编程相当陌生,所以这可能是一个非常简单的错误。无论如何,我有一个子视图,我希望它出现在“正常”视图之上,这样用户可以使用 UIDatePicker 输入日期并使用 UIToolbar 中的 UIBarButtonItem 确认他的选择(这也会提示子视图消失) . 问题是控制元素(选择器和按钮)不响应点击和其他手势,尽管在“正常”视图的顶部可见。我注意到作为“正常”视图一部分的按钮会做出响应,即使该按钮位于子视图下方并且不可见。这是生成子视图及其元素的代码:

UIView * newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 0)];
newView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];

UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 49)];
toolbar.barStyle = UIBarStyleBlack;

UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:nil action:@selector(dismissCustom:)];
toolbar.items = [NSArray arrayWithObjects:infoButtonItem, nil];

UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
datePicker.frame = CGRectMake(0, 49, 320, 250);
datePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[datePicker addTarget:self action:@selector(pickerDateChanged:) forControlEvents:UIControlEventValueChanged];
[newView addSubview:datePicker];

[newView addSubview:toolbar];
[self.view addSubview:newView];

[self.view bringSubviewToFront:newView];

newView.tag = 1;

方法dismissCustom:和方法pickerDateChanged:如下:

-(void)pickerDateChanged:(UIDatePicker *)sender
{
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];

    NSLog(@"Picked the date %@", [dateFormatter stringFromDate:[sender date]]);

    dateButton.titleLabel.text = [dateFormatter stringFromDate:sender.date];
}

-(void)dismissCustom:(UIBarButtonItem *)sender
{
     UIView * newView = [[UIView alloc] viewWithTag:1];
     [newView removeFromSuperview];
}

有人知道缺少什么/错了吗?任何帮助表示赞赏!干杯

4

1 回答 1

2

至少对于按钮,您需要为操作添加一个 touchUpInside 事件:

[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
于 2013-01-16T18:33:53.230 回答