Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
UIToolBar上的“完成”按钮不工作-iOS_Ios_Objective C_Uipickerview_Uitoolbar - Fatal编程技术网

UIToolBar上的“完成”按钮不工作-iOS

UIToolBar上的“完成”按钮不工作-iOS,ios,objective-c,uipickerview,uitoolbar,Ios,Objective C,Uipickerview,Uitoolbar,我正在尝试创建一个带有“完成”按钮的条的选择器 我试图实现以下目标: viewForDatePicker = [[UIView alloc]initWithFrame:CGRectMake(0, 300, 320, 266)]; [viewForDatePicker setBackgroundColor:[UIColor whiteColor]]; UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 32

我正在尝试创建一个带有“完成”按钮的条的选择器

我试图实现以下目标:

viewForDatePicker = [[UIView alloc]initWithFrame:CGRectMake(0, 300, 320, 266)];

[viewForDatePicker setBackgroundColor:[UIColor whiteColor]];

UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;

UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                     target:self action:@selector(doneButtonPressed:)];

UIButton * doneButton =[[UIButton alloc]initWithFrame:CGRectMake(290, 2, 30, 20)];
[doneButton setBackgroundColor:[UIColor redColor]];
[doneButton addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[viewForDatePicker addSubview:doneButton];

[toolBar setItems:[NSArray arrayWithObject:btn]];
[viewForDatePicker addSubview:toolBar];

birthDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 320, 266)];
[birthDatePicker setDatePickerMode:UIDatePickerModeDate];
[birthDatePicker setBackgroundColor:[UIColor whiteColor]];

[viewForDatePicker addSubview:birthDatePicker];


[self.view addSubview:viewForDatePicker];
遗憾的是,“完成”按钮不执行。这个代码有什么问题


你能帮我一下吗?

你先把按钮添加到视图中。 因此,当您添加日期选择器时,它位于按钮上方。这就是按钮对触摸不响应的原因。 更改为:

birthDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 320, 266)];
[birthDatePicker setDatePickerMode:UIDatePickerModeDate];
[birthDatePicker setBackgroundColor:[UIColor whiteColor]];

[viewForDatePicker addSubview:birthDatePicker];

UIButton * doneButton =[[UIButton alloc]initWithFrame:CGRectMake(290, 2, 30, 20)];
[doneButton setBackgroundColor:[UIColor redColor]];
[doneButton addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[viewForDatePicker addSubview:doneButton];

[toolBar setItems:[NSArray arrayWithObject:btn]];
[viewForDatePicker addSubview:toolBar];

你能分享doneButtonPressed的代码吗?还想提醒您,您在“btn”和“doneButton”中使用相同的选择器方法。日期选择器是否位于按钮顶部?尝试在日期选择器之后添加按钮。@CW0007007如果您添加了答案,则您的答案是正确的。标记为对其他人正确。