gpt4 book ai didi

iOS subview (UIDatePicker 和 UIBarButtonItem)不响应用户输入

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:16:36 26 4
gpt4 key购买 nike

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

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];
}

有没有人知道缺少/错误的地方?任何帮助表示赞赏!干杯

最佳答案

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

[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];

关于iOS subview (UIDatePicker 和 UIBarButtonItem)不响应用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14365026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com