How to dismiss UIActionSheet with close button and avoid crash?
12,273
Just implement dismissActionSheet
and put your dismiss message there.
The dismissActionSheet
method would look something like this:
-(void)dismissActionSheet {
[sheet dismissWithClickedButtonIndex:0 animated:YES];
}
Author by
Shaddy
Updated on June 05, 2022Comments
-
Shaddy about 2 years
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?
-
Mundi over 12 yearsDid you not define the method
-(void)dismissActionSheet:(id)sender
? You defined it in youraddTarget
method of the button. If you tell it that the selector isdismissActionSheet:
you will also have to write one ;-). In this method, calldismissWithClickedButtonIndex:animated:
. -- All clear? -
Shaddy over 12 yearsThanks 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:?
-
Mundi over 12 yearsRight. The function does not know what type the sender is, (just
id
), so you have to cast it. See the edit of my answer. -
Shaddy over 12 yearsMundi, I tried out your suggestion and was hopeful. However, it still crashes. Isn't the sender a button and not a UIActionsheet?
-
Mundi over 12 yearsIndeed. 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.
-
Shaddy over 12 yearsIt worked... Mundi you are the man! Simply created an instance variable actionSheetRef and assigned actionSheetRef=actionSheet;