0

I need to show a datePicker when a textField is clicked. Here is the code:

- (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)];
}

When showInView is called, a lot of errors are shown in console:

CGContextSetFillColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

Then the same message with: CGContextSetStrokeColorWithColor CGContextSaveGState CGContextSetFlatness CGContextAddPath CGContextDrawPath CGContextRestoreGState

Why this happens?

cicaletto79
  • 179
  • 1
  • 12

1 Answers1

1

After running your code, I found the problem is caused by the nil title of UIActionSheet. A workaround is modified as below.

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

Another good resource for your reference.

Nathan Chang
  • 436
  • 2
  • 10
  • Found related issue [here](http://stackoverflow.com/questions/19011170/invalid-context-error-on-ios-7-when-adding-a-uipickerview-inside-a-uiactionsheet). – Nathan Chang Jun 11 '14 at 10:07