How to dismiss UIActionSheet with close button and avoid crash? - objective-c

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?

Just implement dismissActionSheet and put your dismiss message there.
The dismissActionSheet method would look something like this:
-(void)dismissActionSheet {
[sheet dismissWithClickedButtonIndex:0 animated:YES];
}

Related

UIButton addTarget method...not working?

I have a class variable that's an instance of UIActionSheet, called actionSheet. I add an instance of UIPickerView as well as an instance of UISegmentedControl. Though when I tap the "Done" button (instance of UISegmentedControl), my class method dismissActionSheet is never called. Can anyone help me out?
//########################################
-(IBAction)chooseTopicButtonPressed{
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 = singlePicker.dataSource;
pickerView.delegate = singlePicker.delegate;
[actionSheet addSubview:pickerView];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(10, 6, 300, 30);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blueColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventTouchUpInside];
//dismissActionSheet NOT BEING CALLED FOR SOME REASON!!!
[actionSheet addSubview:closeButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
-(void)dismissActionSheet{
NSLog(#"dismissActionSheet called");
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
The method dismissActionSheet doesn't have any arguments so the #selector should be as follows:
[closeButton addTarget:self action:#selector(dismissActionSheet) forControlEvents:UIControlEventTouchUpInside];

hidden navigation bar causing trouble with action sheet

The navigation bar in my uinavigationbarcontroller is hidden, but my actionsheet thinks its still there, hence giving me a the highlights at the top of the action page (seen above). How can I erase this/ trick my actionsheet into thinking its a normal uiimageview?
Navigation bar hidden like so in didLoad:
[self.navigationController setNavigationBarHidden:YES animated:NO];
and:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (![self.navigationController isNavigationBarHidden])
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
}
actionsheet looks like this in didLoad:
actionSheet = [[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 200,320,16)];
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done_clicked:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
UIPickerView *locationPicker = [[UIPickerView alloc] init];
locationPicker.frame = CGRectMake(0, 244, 320, 216);
locationPicker.delegate = self;
locationPicker.dataSource = self;
locationPicker.showsSelectionIndicator = YES;
locationPicker.tag = 1;
[actionSheet addSubview:locationPicker];
Then its animated in on textfield touch.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == locationTextField.self)
{
[actionSheet showInView:self.view];
[actionSheet setFrame:CGRectMake(0, 0, 320, 480)];
[nameField resignFirstResponder];
}
}
I believe the problem is with the view you chose to show the action sheet. try to show it in your app's main window.

UIActionSheet comes up halfway

I was looking for a way to make a UIPickerView show a "Done", "Cancel" buttons.
Googling around I found good posts, all of them had UIActionSheet involved.
So I did that
container is a UIView
actionSheet is a UIActinSheet
pickerView is a UIPickerView
in viewDidLoad:
container = [[[UIView alloc] initWithFrame:CGRectMake(rect.origin.x,rect.origin.y, rect.size.width, rect.size.height)] autorelease];
[self.view addSubview:container];
actionSheet = [[UIActionSheet alloc] initWithTitle:#"myTitle"
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
pickerView = [[[UIPickerView alloc] initWithFrame:pickerFrame]autorelease];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
UIToolbar *toolbar = [[[UIToolbar alloc] init] autorelease];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[[NSMutableArray alloc] init] autorelease];
[items addObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(dismissActionSheet)] autorelease]];
[items addObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:#selector(goActionSheet)] autorelease]];
[items addObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(goActionSheet)] autorelease]];
[toolbar setItems:items animated:NO];
[actionSheet addSubview:toolbar];
[actionSheet addSubview:pickerView];
[actionSheet showInView:container];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
The app starts great, and the whole thing shows up nice as supposed to be on the line: [actionSheet showInView:container];
Now, When I try to dismiss the ActionSheet, it works great!
in dismissActionSheet:
[actionSheet dismissWithClickedButtonIndex:1 animated:NO];
The problem is, how do I make it show up again?
When I use:
[actionSheet showInView:container];
I only get the Toolbar picking from the bottom with the buttons and that's it. The whole PickerView is at the bottom and I can't see it.
What am I doing wrong here?
Action sheets are intended to be used more like alerts, in that they are standalone "make a choice now" presenters. They do normally only display part-way on the iPhone.
Your picker should be independent of the action sheet. If you want to have "Done" and "Cancel" buttons perhaps you would want to consider adding them to the picker's navigation bar?
You can use UIToolBar having Done and Cancel Button, with your PickerView, this will be easy.

selector for a UIDatePicker with args (UITextfiled)

I've seen a lot of text fields with triggering a datepicker.
I want have only one method to make a datepicker. This datepicker must have a method selector with the identifier of the text fields. I don't know how can i make this.
Here is my code:
- (IBAction)launchPickerDate:(id)sender{
UIDatePicker *datepickerNew = [UIDatePicker new];
[datepickerNew setHidden:NO];
datepickerNew.frame = CGRectOffset(datepickerNew.frame, 0, 244);
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
UIDatePicker *launchPickerDate = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[launchPickerDate addTarget:self action:#selector(SelectDatePicker:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:launchPickerDate];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Ok"]];
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(closeDatePicker:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
I didn't really get your question but it seems to me you want to make some changes to your date picker based upon the text field selected. You can recognize the textfield from which the method was called using the tag property. Like
if (sender.tag==1){
//do something as first text field selected
}else if(sender.tag==2){
//do something as second text field selected
}
you can specify the tags for text field either by interface builder(if you are using xib's) or via code like. firstTextField.tag=1;
and so on.. hope this helps... if anything else just let me know

ActionSheet in NavController with TabBarController - Exc_Bad_Access

I'm currently have a weird problem.
I have an actionsheet with pickerView in with two button (cancel, and Ok).
They work perfectly until i choose a value in that picker and i click on the first button of my TabBarController which contain my NavigationController.
My actionsheet is declared in my .h and has property (nonatomic, retain), i release it in my dealloc function.
I have an exc_bad_access if i release this object and i dont know why because i allocated it.
This is my function which create my Actionsheet if is nil, and build it.
- (IBAction)select_marque :(id)sender
{
if (!actionSheet_marque)
{
actionSheet_marque = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
}
[actionSheet_marque setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
picker_marque = [[UIPickerView alloc] initWithFrame: pickerFrame];
picker_marque.showsSelectionIndicator = YES;
picker_marque.dataSource = self;
picker_marque.delegate = self;
[actionSheet_marque addSubview: picker_marque];
[picker_marque release];
UILabel *lbl = [[UILabel alloc] initWithFrame: CGRectMake(110, 7.0f, 150.0f, 30.0f)];
lbl.text = #"Marque";
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
[actionSheet_marque addSubview:lbl];
[lbl release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Ok"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blueColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet_marque addSubview:closeButton];
[closeButton release];
UISegmentedControl *cancelButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Cancel"]];
cancelButton.momentary = YES;
cancelButton.frame = CGRectMake(5, 7.0f, 50.0f, 30.0f);
cancelButton.segmentedControlStyle = UISegmentedControlStyleBar;
cancelButton.tintColor = [UIColor redColor];
cancelButton.tag = 1;
[cancelButton addTarget:self action:#selector(cancelActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet_marque addSubview:cancelButton];
[cancelButton release];
[actionSheet_marque showInView: self.navigationController.view];
[actionSheet_marque setBounds:CGRectMake(0, 0, 320, 485)];
}
I check also if the pointer is nil in dealloc method....
Thanks
If you have property of UIActionsheet then you should use self.actionSheet_marque when you allocate it. Try changing code like this
if (!actionSheet_marque)
{
self.actionSheet_marque = [[[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil] autorelease];
}
[self.actionSheet_marque setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
And in whole function except this line if (!actionSheet_marque)