2014-06-11 42 views
0

我需要在單擊textField時顯示一個datePicker。 下面是代碼:dateSheet上的UIDatePicker錯誤showInView

- (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

爲什麼出現這種情況?

回答

1

運行您的代碼後,我發現問題是由UIActionSheet的無標題引起的。 解決方法修改如下。

dateSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil]; 

另一個不錯的resource供您參考。

+0

找到相關問題[here](http://stackoverflow.com/questions/19011170/invalid-context-error-on-ios-7-when-adding-a-uipickerview-inside-a-uiactionsheet)。 –

相關問題