Perform UIActionSheet action's on different View Controller? - ios

I have a slide out menu in my app, which has a button that will display a UIActionSheet. This slide out menu just partly covers whatever view controller you previously were on.
- (void)cameraButtonPushed:(UIButton *)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Create String" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Take Photo", #"Choose from Library", nil];
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
//[actionSheet showInView:self.parentViewController.view];
}
As you can see I have tried two ways of showing the action sheet and they both appear to work the same.
The problem that I'm having has to do with the action sheet buttons actually working. It shows up in the parent view controller (view controller that you were on), but if I press any of the buttons they do not work.
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
EditVC *newEditVC = [self.storyboard instantiateViewControllerWithIdentifier:#"editVC"];
[editVC setHidesBottomBarWhenPushed:YES];
[self.parentViewController.navigationController pushViewController:newEditVC animated:YES];
//[self.navigationController pushViewController:newEditVC animated:YES];
} else if (buttonIndex == 1) {
EditVC *newEditVC = [self.storyboard instantiateViewControllerWithIdentifier:#"editVC"];
[editVC setHidesBottomBarWhenPushed:YES];
[self.parentViewController.navigationController pushViewController:newEditVC animated:YES];
//[self.navigationController pushViewController:newEditVC animated:YES];
}
}
Both of the above methods are in the same menu view controller where the button is pressed. I put some break points to test it out and the action sheet delegate method is being called. The if statement is working correctly, but the new VC's are not being pushed onto the navigation controller the way they should. It just runs over those lines and nothing happens. I have also tried setting it up in a way that parent view controller's also have the actionSheet:clickedButtonAtIndex: method in them, but that method is not called.
I believe that it has to do with setting up the actionSheet with an improper delegate, but I don't know for sure.
How can I call this action sheet from one view controller, display it and have actions performed in another? I have it appearing in the correct VC, but the action's aren't working.

Related

UIActionSheet throws ViewController actionSheet:clickedButtonAtIndex

I am using XCode 5.1.1 for development purposes and using the iOS simulator to test my app.
I have an action sheet that has Save, Cancel and Delete options. I haven't yet coded the action handling for Save and Delete, but tapping on cancel should go back to the previous screen (I have used navigation controller to move between screens). But tapping on cancel, throws me "ViewController actionSheet:clickedButtonAtIndex:]: message sent to deallocated instance" error, which I am not able to solve. I have hooked up all the elements to their outlets/actions correctly. Below is my code. Please help. (I am trying to display the action sheet when a "return" button is clicked. And in the action sheet, when I tap cancel, the previous screen had to be displayed - I guess this can be done by dismissViewControllerAnimated which dismisses the current controller and displays the previous controller.)
-(IBAction)returnModalAction:(id)sender {
[self dismissViewControllerAnimated: YES completion: NULL];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"What do you want to do with the user values?"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:#"Delete"
otherButtonTitles:#"Save", nil];
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"Cancel"];
[actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 2) {
NSLog(#"You have pressed the %# button", [actionSheet buttonTitleAtIndex:buttonIndex]);
}
}
On your method returnModalAction: you are dismissing the view, so the garbage collector will release all the references of self(the view controller) in memory that's why when you try to to show the action sheet [actionSheet showInView:self.view]; you get the error because the reference in memory doesn't exist.
So what you have to do is to perform [self dismissViewControllerAnimated: YES completion: NULL]; when you actually want to display the previous screen, in your case that would be on the actionSheet:clickedButtonAtIndex: method, based on the index of the button.

UIAlertview button action to change to another viewcontroller?

I am just getting into Xcode programming, and I need code so that when I click a button, it is a confirmation to take me into another ViewController.
I have an X button on the page, and when clicked I need a UIAlertview to pop up saying 'cancel' and 'next', and when next is pressed, i want it to change to another ViewController. These cancel and next buttons need to be side by side.
First thing you need to do is make sure the view with a button is a delegate of UIAlertView.
#interface ViewController() <UIAlertViewDelegate>
Then you need to add a touch up inside action to the button in your view. When the button is pressed, it creates the alertView and shows it. Note, its delegate is self.
- (IBAction)clickButton:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Test" message: #"Message" delegate: self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Next", nil];
[alert show];
}
Lastly, you create the delegate function. This is triggered whenever a button is clicked in the alertView. buttonIndex corresponds to the button that was clicked, so we want to present the view when the 1st button is pressed (0 corresponds to cancel, 1 corresponds to next). Realistically, you'd probably want to present a custom view controller that you define.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1){
UIViewController* newView = [[UIViewController alloc] init];
[self presentViewController:newView animated:YES completion:nil];
}
}

Why clickedButtonAtIndex method is call even tap on out side of UIActionSheet?

As my title said that clickedButtonAtIndex delegate method is call even tap on out side of UIActionSheet.
My actionSheet is display when I tapped on UIButton.
Code of my UIActionSheet declaration.
-(void)btnComplexityTapped:(UIButton *) sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select your complexity" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Easy", #"Medium", #"High", nil];
[actionSheet showFromRect:sender.frame inView:self.view animated:YES];
[actionSheet release];
}
And in delegate method
- (void)actionSheet:(UIActionSheet *)myActionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
[self.btnComplexity setTitle:#"Easy" forState:UIControlStateNormal];
}
else if(buttonIndex == 1)
{
[self.btnComplexity setTitle:#"Medium" forState:UIControlStateNormal];
}
else
{
[self.btnComplexity setTitle:#"High" forState:UIControlStateNormal];
}
}
I know that this issue is fix by set last condition in delegate method to set else if(buttonIndex == 1) instead of only else. But I does not want to apply any fixing but I want to know why delegate method is called even I am not tapping on any button of UIActionSheet
See this video for more clear picture.
As above video default title of button is "Medium" when I click on button then actionSheet is open and I click on out side of actionSheet (such like self.view) then delegate method clickedButtonAtIndex is called so last else condition is become true and thats way my button's title is changed "High".
Can any body tell my why this is happening ?
In iPad action sheet is presented in a popover. No cancel button because touching out of popover is equivalent to a touch on cancel. So, your delegate receives the message as if cancel button has been touch.
Because it is presented in POPOVER (iPad) which does not have cancel button on ActionSheet.Its Cancel is triggered if you tap out of ActionSheet.

Dismissing both UINavigation views and Modal views at once programmatically

I have tried a few answers on this site, but none of them seem to relate to my problem
I have a MasterDetail app which has a two types of segues that I am using. When you press a button on the detail view, it uses a push segue and pushes another detail view onto that. In the new detailview (the one that was just pushed on) there is a button which calls up another UIView (form sheet) using a modal segue.
What I am trying to do is when the user selects a row, a UIAlertView will show up displaying a message, while at the same time (doesn't have to be at the same time) it dismisses the UIViewcontroller (modal) and goes back from the Viewcontroller that was pushed on. Basically, I need to be able to dismiss all viewcontrollers, one modal and one push (nav) so that the view returns to the original main screen that they started with.
I have the UIAlertView working fine and I can get the modal viewcontroller to dismiss by using [self.dismissViewControllerAnimated:YES completion:nil]; but I don't know how to dismiss the next Viewcontroller (which is in a navigation controller). Using this: [self.navigationController popToRootViewControllerAnimated:NO]; does not work.
Here is where I want to call the function to remove all views:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlWithIDAndChallenge];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
UIAlertView *message = [[UIAlertView alloc] initWithTitle#"Started" message:#"The challenge has begun!" delegate:nil cancelButtonTitle:#"OK!" otherButtonTitles:nil];
[message show]
//right here is where I would normally dismiss the modal VC
//here is where I would like to dismiss all VC
}
If you want, in iOS 6.0 (and later) projects you can use an unwind segue. For example, you can:
In your top level view controller (the one you want to unwind to, not the controller you're going to unwind from), write an unwind segue method, in this case called unwindToTopLevel (personally, I find it useful if the segue bears some indication as to what the destination of the segue is, for reasons that will become apparent when we get to step 3, below):
- (IBAction)unwindToTopLevel:(UIStoryboardSegue *)segue
{
NSLog(#"%s", __FUNCTION__);
}
In the Interface Builder scene from which you will initiate the unwind, control ⌘-drag from the view controller icon to the exit icon to create the unwind segue:
Generally you'd define the segue from a button to the exit outlet (and you're done), but if you want to invoke the segue programmatically, you might want to create it between the controller and the unwind outlet, like shown above.
You'll get a little pop up that includes the name of your unwind segues (from the presenting controllers ... it's like magic):
If you're going to invoke that segue programmatically, you have to select that unwind segue in the document outline on the left side of the IB window and once you've done that, you can give the unwind segue a storyboard id:
I generally use the name of the the unwind segue for the storyboard id, but you can use whatever you want.
Now, having done that, your alert view can invoke the segue:
- (IBAction)didTouchUpInsideButton:(id)sender
{
[[[UIAlertView alloc] initWithTitle:nil message:#"go home" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil] show];
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex])
{
[self performSegueWithIdentifier:#"unwindToTopLevel" sender:self];
}
}
From the class you presented your modal view
call dismiss of modal and then perform selector after some delay and then do the
here is the sample code
//Write this in the class from where you presented a modal View.
//Call the function from modal class when you want to dismiss and go to root.
- (void) dismissAndGoToRoot {
[self dismissViewControllerAnimated:YES completion:nil];
[self performSelector:#selector(gotoRoot) withObject:nil afterDelay:0.50];
}
- (void)gotoRoot {
[self.navigationController popToRootViewControllerAnimated:NO];
}
Here you will call the function
//right here is where I would normally dismiss the modal VC
//here is where I would like to dismiss all VC
[self.parentViewController dismissAndGoToRoot];
If this does not work then take an instance variable of ParentViewController in modal class and assign when presenting modal view controller.
You could try replacing [self.dismissViewControllerAnimated:YES completion:nil]; with
self dismissViewControllerAnimated:YES completion:^{
[parentViewController.navigationController popToRootViewControllerAnimated:YES];
}
I believe parentViewController will point to the presenting view controller, and the block will cause the parent view controller's navigation controller to pop to the root view controller.

How do I present a modal view upon an option in UIActionSheet being tapped?

I have my main view controller, and tapping a button brings up an action sheet. I want one of the options tapped there to bring up an action sheet. I can't seem to figure it out, however, despite all my searching.
I have the following code:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Text"]) {
AddTextViewController *addTextViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"addTextViewController"];
}
Now that I instantiated the view controller, I don't know what to do.
Add this line right after you instantiate it.
if ([buttonTitle isEqualToString:#"Text"]) {
AddTextViewController *addTextViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"addTextViewController"];
[self presentViewController:addTextViewController animated:YES completion:nil];
}
Note
You can also create a custom segue in the Storyboard that presents your view, then instead of instantiating and presenting you can just perform your custom segue. Both work the same.
Storyboard Method
First click your segue in Storyboards, then give it an identifier in the info panel.
Then you can replace your code with this and it should trigger the segue from your Storyboard.
if ([buttonTitle isEqualToString:#"Text"]) {
[self performSegueWithIdentifier:#"infoSegue" sender:self];
}

Resources