我对iOS编程很新,所以这可能是一个非常简单的错误。无论如何,我有一个子视图,我希望它出现在“普通”视图之上,以便用户可以使用UIDatePicker输入日期并使用UIBarButtonItem确认他的选择(这也会提示子视图消失)在UIToolbar中。问题是控制元素(Picker和Button)不响应轻敲和其他手势,尽管在“正常”视图的顶部可见。我注意到,作为“普通”视图一部分的按钮会响应,即使此按钮位于子视图下方且不可见。以下是生成子视图及其元素的代码:
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];
}
有人知道遗失/错误的是什么吗?任何帮助表示赞赏! 干杯
答案 0 :(得分:2)
至少对于按钮,您需要为操作添加touchUpInside事件:
[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];