2
votes

I have a UIActionSheet to select a time for use in my app. Selecting the time works fine, but I can't find a way to hide the action sheet when the "Done" button is clicked.

The code I have is:

-(void) showTimespanPicker{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Preparation Time", "Preparation Time")
    delegate:self                                    cancelButtonTitle:nil//@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    actionSheet.tag = PREPTIME_PICKER;

    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
    prepTimePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
    prepTimePicker.datePickerMode = UIDatePickerModeCountDownTimer;
    prepTimePicker.countDownDuration = (NSTimeInterval )[recipe.prepTime doubleValue];

    [actionSheet addSubview:prepTimePicker];
    [prepTimePicker release];

    // create toolbar with "Done" button
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(prepTimeSelected)];
    NSArray *barButtons = [NSArray arrayWithObject:barButtonItem];
    [toolbar setItems:barButtons];
    [actionSheet addSubview:toolbar];

    MyAppAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    [actionSheet showInView:delegate.window];
    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
// fires when the done button is clicked
-(void)prepTimeSelected {
    recipe.prepTime = (NSInteger)prepTimePicker.countDownDuration;
    [self updatePickerValues];
    [[prepTimePicker.superview].setHidden:YES];
}

The final line in prepTimeSelected hides the UIDatePicker, but not the actionsheet. I think I need some sort of recursive way of finding the parent of the button, either that or create a class variable/property for the ActionSheet, but that way feels wrong.

3
did you ever get a solution for this? I'm having the same problem. - Brian Driscoll

3 Answers

0
votes
UIActionSheet *menuProperty;    

@property(nonatomic,retain) UIActionSheet *menuArea;  

Then you need to make following changes in your .m file

menuArea = [[UIActionSheet alloc] initWithTitle:nil  delegate:self
                                        cancelButtonTitle:@"Done"  
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:nil];  


// Add the picker  
pickerArea = [[UIPickerView alloc] initWithFrame:CGRectMake(0,185,0,0)];  

pickerArea.delegate = self;  
pickerArea.showsSelectionIndicator = YES;    // note this is default to NO  

[menuArea addSubview:pickerArea];  
[menuArea showInView:self.view];  
[menuArea setBounds:CGRectMake(0,0,320, 600)];  
0
votes
UIView *aView = prepTimePicker;
do {
    aView = aView.superView;
} while ((aView != nil) && [aView isMemberOfClass:[UIActionSheet class]]);
[aView dismissWithClickedButtonIndex:0 animated:YES];

This should help you find the UIActionSheet object.

0
votes
[[ [[[UIApplication sharedApplication] delegate] window]  
   viewWithTag: PREPTIME_PICKER] dismissWithClickedButtonIndex:0 animated:YES];

This didn't work?