How to dismiss UIActionSheet with close button and avoid crash?
Asked Answered
G

1

6

I have read on another post (how to dismiss action sheet) that I can use

[actionSheet dismissWithClickedButtonIndex:0 animated:YES];

to dismiss the uiactionsheet with the close button, as defined in:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                         delegate:nil
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:nil];

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

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

UIPickerView *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];


[actionSheet dismissWithClickedButtonIndex:0 animated:YES];


[closeButton release];


[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

However, I am not sure where to place this line, assuming this will solve my problem:

[actionSheet dismissWithClickedButtonIndex:0 animated:YES]; 

Also, the app crashes when I click the close button, with error message "PROGRAM RECEIVED ERROR MESSAGE SIGBRT". I assume my two problems are related. Any help out there?

Greenes answered 4/12, 2011 at 20:54 Comment(0)
A
10

Just implement dismissActionSheet and put your dismiss message there.

The dismissActionSheet method would look something like this:

-(void)dismissActionSheet {
   [sheet dismissWithClickedButtonIndex:0 animated:YES];
}
Artichoke answered 4/12, 2011 at 23:14 Comment(6)
Did you not define the method -(void)dismissActionSheet:(id)sender? You defined it in your addTarget method of the button. If you tell it that the selector is dismissActionSheet: you will also have to write one ;-). In this method, call dismissWithClickedButtonIndex:animated:. -- All clear?Artichoke
Thanks Mundi, I'm almost there. I have implemented -(void)dismissActionSheet:(id) sender{ [sender dismissWithClickedButtonIndex:0 animated:YES]; } However, I am sure sender is not what I need to put before the 'dismissWithClickedButtonIndex. I need to reference the actionSheet. How do I reference the actionSheet when it is not an argument in the function dismissActionSheet:?Greenes
Right. The function does not know what type the sender is, (just id), so you have to cast it. See the edit of my answer.Artichoke
Mundi, I tried out your suggestion and was hopeful. However, it still crashes. Isn't the sender a button and not a UIActionsheet?Greenes
Indeed. The best solution is to keep a reference of your action sheet elsewhere (put it into your .h file and assign to it the action sheet when you create it) and not using the sender at all.Artichoke
It worked... Mundi you are the man! Simply created an instance variable actionSheetRef and assigned actionSheetRef=actionSheet;Greenes

© 2022 - 2024 — McMap. All rights reserved.