views:

82

answers:

1

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.

+2  A: 

This SO post has a solution to adding a UIDatePicker to a UIActionSheet that might help you directly. Or this post, looks better.

Personally, I could never get any of these solutions to work nicely and I'm not sure it would be the approach Apple would suggest? I had complete success and unity with how these UIPickerView views look and behave in Apple apps by animating a UIView, with your UIPickerView as a subview, yourself - this blog post helped me get started. When I got this behaviour working I then added an OverlayViewController helper class to make my (modal) view background semi-transparent; see post.

See post regarding wait_fences question.

petert