我正在尝试使用UIToolBar显示UIPickerView但是出现了一些错误。
这是我的代码 -
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);
UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds];
darkView.alpha = 0;
darkView.backgroundColor = [UIColor blackColor];
darkView.tag = 9;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)];
[darkView addGestureRecognizer:tapGesture];
[self.view addSubview:darkView];
UIDatePicker *picker = [[UIDatePicker alloc] init];
picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
picker.datePickerMode = UIDatePickerModeDate;
[picker addTarget:self action:@selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
[picker setFrame:CGRectMake(0,235,320,120)];
picker.backgroundColor = [UIColor blackColor];
[self.view addSubview:picker];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
toolBar.tag = 11;
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] ;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)];
[toolBar setItems:@[spacer, doneButton]];
[self.view addSubview:toolBar];
[UIView beginAnimations:@"MoveIn" context:nil];
toolBar.frame = toolbarTargetFrame;
picker.frame = datePickerTargetFrame;
darkView.alpha = 0.5;
[UIView commitAnimations];
在这一行上获取错误 -
picker.frame = datePickerTargetFrame;
这是错误 -
*** Assertion failure in -[UIPickerTableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:7768
2013-10-03 13:43:12.688 Mistoh Beta 1[7228:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource is not set'
libc++abi.dylib: terminating with uncaught exception of type NSException
请帮助我。提前谢谢你。
答案 0 :(得分:17)
我遇到了同样的问题,从iOS7.03开始出现了一些崩溃。
它可以通过移动来解决
[self.view addSubview:picker];
在你的例程结束时,基本上是在设置了选择器的框架之后。即
picker.frame = datePickerTargetFrame;
[self.view addSubview:picker];
答案 1 :(得分:5)
我使用UIDatePicker作为inputView,所以Tao-Nhan接受的答案对我没用。我很长一段时间一直在努力解决这个问题,但是今天我终于找到了一个优雅的解决方案!
经过大量调查后,我发现在输入视图上调用didMoveToSuperview之后发生了崩溃。诀窍是使用'包装'使用UIDatePicker作为子视图作为inputView进行查看,并在从superview中删除inputView时删除选择器,并在将其移动到新的superview后在runloop上的下一次运行时重新添加它。如果这听起来太混乱,只需使用下面的代码作为输入视图,你就可以了。
<强> GSDatePickerInputView.h 强>:
#import <UIKit/UIKit.h>
@interface GSDatePickerInputView : UIView
@property (nonatomic, readonly) UIDatePicker *datePicker;
@property (nonatomic) BOOL useWorkaroundToAvoidCrash;
@end
<强> GSDatePickerInputView.m 强>:
#import "GSDatePickerInputView.h"
@interface GSDatePickerInputView ()
@property (nonatomic, strong, readwrite) UIDatePicker *datePicker;
@end
@implementation GSDatePickerInputView
- (instancetype)init {
if (self = [super initWithFrame:CGRectMake(0, 0, 320, 166)]) {
self.translatesAutoresizingMaskIntoConstraints = NO;
self.backgroundColor = [UIColor whiteColor];
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
datePicker.backgroundColor = [UIColor whiteColor];
[self addSubview:datePicker];
self.datePicker = datePicker;
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.datePicker.frame = self.bounds;
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
if (self.useWorkaroundToAvoidCrash == YES) {
if (newSuperview == nil) {
[self.datePicker removeFromSuperview];
}
}
}
- (void)didMoveToSuperview {
if (self.useWorkaroundToAvoidCrash == YES) {
if (self.superview != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[self addSubview:self.datePicker];
self.datePicker.frame = self.bounds;
});
}
}
}
@end
关键部分是方法willMoveToSuperview:
和didMoveToSuperview
。 {/ 1}} GCD函数用于在发生崩溃后将datePicker返回。
然后,您可以像这样使用此inputView的实例:
dispatch_async
您可以稍后使用以下代码访问datePicker:
GSDatePickerInputView *dateInputView = [[GSDatePickerInputView alloc] init];
dateInputView.useWorkaroundToAvoidCrash = YES;
[dateInputView.datePicker addTarget:self action:@selector(datePickerChanged:) forControlEvents:UIControlEventValueChanged];
yourView.inputView = dateInputView;
最后一个注意事项 - 属性((GSDatePickerInputView *)yourView.inputView).datePicker
适用于在一个地方崩溃的情况,但在另一个地方它不是(发生在我身上)。尽可能避免这种hackery显然会更好,所以只有在它实际崩溃的地方才将此属性设置为YES。
答案 2 :(得分:4)
我遇到过类似的问题,我必须更改maximumDate
上minimumDate
和UIDatePicker
inputView
UITextField
上UITableView
的子视图UIDatePicker
1}}在我的CORS
中,经过大量跟踪后,似乎问题是同时设置两个日期。我能够实现这一目标的唯一方法是从我的自定义输入视图中完全删除OPTIONS
并创建并重新添加它并设置新的最小和最大日期值。这是我在很长一段时间内被迫做的最丑陋的事情。
答案 3 :(得分:3)
UIDatePicker在内部管理UIPicker。因此,消息中包含UIPicker。 现在来看实际的错误信息: 当您尝试显示DatePicker时,您是否有足够的空间? 抛出此错误的原因之一是,如果找不到足够的空间来显示整个视图。
答案 4 :(得分:2)
在自定义UITableViewCell中遇到与UIPickerView相同的问题。我已经移动了处理将Picker视图放置在父视图上的所有逻辑以及设置它对- (void)layoutSubviews
更新后,它看起来像下一个方式:
- (void)layoutSubviews
{
[super layoutSubviews];
[self addSubview:YOUR_PICKER_VIEW]; //'self' in my case was UITableViewCell
[self addConstraints:PICKER_VIEW_CONSTRAINTS];
}
答案 5 :(得分:0)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (textField == _txtFieldEndDate)
{
_datePicker.maximumDate = [NSDate dateWithDaysFromNow:100 * 365];
[_datePicker setDate:[NSDate date]];
}
else if (textField == _txtFieldStrtDate)
{
_datePicker.maximumDate = [NSDate date];
}
});