OGeek|极客世界-中国程序员成长平台

标题: ios - 带有 UIToolbar 错误的 UIDatePicker [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:26
标题: ios - 带有 UIToolbar 错误的 UIDatePicker

我正在使用 UIDatePicker 并在单击 UITextField 时实现 UIToolBar。但是当我单击文本字段时,它会崩溃。我正在使用以下代码。

datepicker = [[UIDatePicker alloc] initWithFrame:CGRectZero]; 
[datepicker setDatePickerMode:UIDatePickerModeDate]; 
[datepicker addTarget:self actionselector(changeDate forControlEvents:UIControlEventValueChanged];

UIToolbar *toolbar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,44)]; 
toolbar.barStyle = UIBarStyleDefault; 
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle"Done" style:UIBarButtonItemStyleDone target:self actionselector(dismiss)];

[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];

[datepicker addSubview:toolbar];

textField.inputAccessoryView = toolbar;

textField.inputView = datepicker;

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'lease file a radar on UIKit with this log if you see this assertion. selectorString = setTitle:, exercisedImplementations = { "setTitle:" = ( ); }'

提前致谢。



Best Answer-推荐答案


UITextFieldinputAccessoryView 设置为已经有 superview 的 View 会导致崩溃。

所以,不要将工具栏添加为 UIDatePicker 的 subview 。删除

[datepicker addSubview:toolbar];

然后设置

textField.inputAccessoryView = toolbar; 

工具栏将出现在数据选择器上方。

关于ios - 带有 UIToolbar 错误的 UIDatePicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51696632/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4