The code below creates one UIActionSheet in XCode 3.2.2, iPhone OS 3.1.3. Embedded in the Action Sheet is a date picker.
But it seems create two sheets in XCode 3.2.3, iOS4, with one overlaid on the other.
Also, I get this error in the console (again, only with XCode 3.2.3, iOS4):
wait_fences: failed to receive reply: 10004003
What do you think the issue is?
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField tag] >0 )
{
[self dismissKeyboard];
NSString *dateTitle;
dateFieldNumber = [textField tag];
dateTitle = @"Enter your Birth Date";
NSString *title = @"\n\n\n\n\n\n\n\n\n\n\n\n" ;
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:[NSString stringWithFormat:@"%@%@", title, NSLocalizedString(dateTitle, @"")]
delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Ok", nil];
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
UIDatePicker *datePicker2 = [[UIDatePicker alloc] init];
self.datePicker = datePicker2;
[datePicker2 release];
self.datePicker.date = [NSDate date];
[self.datePicker addTarget:self
action:@selector(changeDateInLabel:)
forControlEvents:UIControlEventValueChanged];
self.datePicker.datePickerMode = UIDatePickerModeDate;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:60];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-60];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps release];
[calendar release];
[self.datePicker setMaximumDate:maxDate];
[self.datePicker setMinimumDate:minDate];
[actionSheet addSubview:self.datePicker];
return NO;
} else {
return YES;
}
}
EDIT
As Brian points out below, the textFieldShouldBeginEditing delegate method was being called twice. I can see no reason why this should be the case in IOS4 and not earlier versions. Anyway, I've changed the code so there is a BOOL hasDisplayedSheet, which initially is NO in viewDidLoad. Then I enclosed the the actionsheet creation code (in the the textFieldShouldBeginEditing delegate method) with an if statement, like
if (hasDisplayedSheet == NO) {
// actionsheet code
hasDisplayed = YES
}
and change hasDiplayedSheet
back to NO when they click the sheet button.