我需要在单击textField时显示datePicker。 这是代码:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self setDate];
return NO;
}
-(void)setDate{
dateSheet = [[UIActionSheet alloc] initWithTitle:Nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
[dateSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 44, 0, 0);
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[datePicker setDatePickerMode:UIDatePickerModeDateAndTime];
NSDate * maxDate = [NSDate dateWithTimeIntervalSinceNow:(3600 * 24 * 14)];
datePicker.maximumDate = maxDate;
datePicker.minimumDate = [NSDate date];
[dateSheet addSubview:datePicker];
UIToolbar * controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];
[controlToolBar setBarStyle:UIBarStyleBlack];
[controlToolBar sizeToFit];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:@"Imposta" style:UIBarButtonItemStyleDone target:self action:@selector(dismissDate)];
setButton.tintColor = [UIColor whiteColor];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Annulla" style:UIBarButtonItemStyleDone target:self action:@selector(cancelDate)];
cancelButton.tintColor = [UIColor whiteColor];
[controlToolBar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil] animated:NO];
[dateSheet addSubview:controlToolBar];
[dateSheet showInView:self.view];
[dateSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
调用showInView时,控制台中会显示很多错误:
CGContextSetFillColorWithColor:无效的上下文0x0。这是一个严重的错误。该应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降低。此通知是礼貌的:请解决此问题。在即将到来的更新中,它将成为一个致命的错误。
然后同样的消息: CGContextSetStrokeColorWithColor CGContextSaveGState CGContextSetFlatness CGContextAddPath CGContextDrawPath CGContextRestoreGState
为什么会这样?
答案 0 :(得分:1)
运行代码后,我发现问题是由UIActionSheet的nil标题引起的。 解决方法修改如下。
dateSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
另一个好resource供您参考。