how to dismiss action sheet
Asked Answered
E

3

11

i used this code to show uipicker in uiactionsheet but when i click close button i want to remove action sheet from view. so what should be the code for removing actionSheet form view.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;

[actionSheet addSubview:pickerView];
[pickerView release];

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
closeButton.momentary = YES; 
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[closeButton release];

[actionSheet showInView:self.view];//[UIApplication mainWindow]];

[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
 return false;
}
Edmund answered 20/4, 2011 at 6:36 Comment(2)
i have used [actionsheet removeFromSuperview]; but its not working.Edmund
see #1552087Clangor
N
11

All you need to do is dismiss the ActionSheet, which you could do with dismissWithClickedButtonIndex:animated:

Nodical answered 20/4, 2011 at 6:52 Comment(1)
What about Swift?Larghetto
M
7

Adding this method worked for me:

    -(void) dismissActionSheet:(id)sender {
        UIActionSheet *actionSheet =  (UIActionSheet *)[(UIView *)sender superview];
        [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
    }
Mauer answered 25/5, 2013 at 19:46 Comment(1)
my sender is a UIBarButtonItem which is added to a mutable array which is added to a UIToolBar which is added to the actionsheet as a subview. How do I access the actionsheet in the dismiss method?Benevolence
E
4

action sheet scope problem.

used [actionSheet dismissWithClickedButtonIndex:0 animated:YES];

Edmund answered 20/4, 2011 at 6:45 Comment(1)
Where did you use that at? I tried to create a dismissActionSheet method, and put the [actionSheet dismissWith...] in it, but it does not seem to be working.Clyde

© 2022 - 2024 — McMap. All rights reserved.