How to dismiss modal view controller using UIAlertView? - iphone

I'd like to dismiss the whole modal view controller by tapping cancel on the UIAlertView that I implemented. The alert view asks the user if he wants to logout and if the user taps Yes, it'll dismiss the modal view controller.
How can I do this in Xcode?
Thanks!

You can start by checking out the documentation on uialertviewdelegate.
First you need to declare your class as the delegate for the uialertview and then implement the method to get the index of button that the user has clicked.
You can use these methods to check the user's choice
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex

Use alert view delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self dismissModalViewControllerAnimated:YES];
}
In case you have more than OK button, you need to mention button index, i.e.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)
{
//Do something
}
if(buttonIndex==1)
{
[self dismissModalViewControllerAnimated:YES];
}
}

If you are using an alertView to dismiss a modal view controller,
Use didDismissWithButtonIndex:(NSInteger)buttonIndex instead of clickedButtonAtIndex:(NSInteger)buttonIndex
The latter causes a crash.

Related

How to create UIAlertView with multiple options?

I am trying to work out how I can use a UIAlertView to do more than one command.
Basically, in my ViewController, there is already an alertView, however I am now adding some storekit files, which, require there own alertView (to tell it whether to purchase the in-app or cancel etc.
Here is original alertView code;
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[alertView dismissWithClickedButtonIndex:0 animated:NO];
if (buttonIndex == 1) {
[g_GameUtils removeAlbumFolder:deleteIndex];
[g_GameUtils readAllData];
[g_GameUtils getAlbumFolderList];
[m_pTable reloadData];
}
}
And here is what I need also - they are both called alertView so I cannot use both like this, is there a way to combine them? Or is it better to call one of them alertView2 ? If so, how does it know which one to call for the particular alert?
Thanks in advance!
Chris
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0){
//cancel button clicked. Do something here or nothing here
}
else{
//other button indexes clicked
[[MKStoreManager sharedManager] buyFeature:#"com.davis.apptoken.buy"];
}
}
Also you can use alertView.tag = 1; and alertView2.tag = 2; and add appropriate conditions to delegate:
if (alertView.tag == 1)
{
// First alert
}

prepareForSegue and UIActionSheet

In my app when the user clicks a button it calls a prepareForSegue. The app need to check a state and then prompt the user if they want to over write or delete. Problem is that it loads the next view controller before the UIActionSheet is displayed. How can I force the UIActionSheet to appear before the prepareForSegue is called? This is my logic for prepareForSegue;
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"mySegue"]) {
//Check Logic removed for simplicity
if ([myCheck count] > 0){
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Save?"
delegate:self
cancelButtonTitle:#"Delete"
destructiveButtonTitle:#"Save"
otherButtonTitles:nil];
[actionSheet showFromToolbar:self.navigationController.toolbar];
}
}
}
Here is the Action sheet;
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == [actionSheet cancelButtonIndex])
{
//Delete logic removed
}else {
//Save logic removed
}
}
You won't be able to do it in a segue. You'll have to load the view controller manually in your action sheet handler when you get the button index that’s supposed to trigger it. That means whatever is triggering the segue now will have to be disconnected and pointed at an IBAction that will create and show your action sheet. prepareForSegue is intended to allow you to set parameters on your destination view controller prior to displaying it. The segue has already been set in motion by the time you get to prepareForSegue. At that point, there is no going back/canceling/delaying the performing of the segue.
Best Regards.
I have found it possible To peform a Segue from a action Sheet
As long as The segue Is from the View controller and and an Identifier here is a example.
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == [actionSheet cancelButtonIndex]) return;
if (buttonIndex == 0) {
[self performSegueWithIdentifier:#"visitWeb" sender:self];
}
if (buttonIndex == 1) {
if ([MFMailComposeViewController canSendMail])
{
Might not be the correct way as I'm a bit of a newb but for my modal view controller on StoryBoard it seems to do the trick
Creating an action sheet is an asynchronous event and does not block the segue. You'll need to trigger the segue from your action sheet.

UIActionSheet from UIAlertView

I'm trying to show a UIActionSheet when the user touches a button in a UIAlertView:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
UIActionSheet *actionSheet = ...
[actionSheet showFromTabBar:self.tabBarController.tabBar];
}
}
When the action sheet is shown the alert view is still on the screen behind the action sheet, and when I touch a button in the action sheet - the action sheet disappears but the whole screen is dimmed with the alert view still on and I can't dismiss it.
I tried several things, such as showing the action sheet after a short delay or dismissing the alert view programmatically, but nothing worked. In the best case (dismissing the alert view programmatically) the alert view did disappear after a somewhat-strange transition but I got a "wait-fence failed to receive reply" error in the log when it did.
How can I show an action sheet from an alert view in an orderly manner?
In this case, you should use
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
method rather than,
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
so your code wil be:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
UIActionSheet *actionSheet = ...
[actionSheet showFromTabBar:self.tabBarController.tabBar];
}
}
Thanks,
Just call dismissWithClickedButtonIndex:animated: method for UIAlertView
if (buttonIndex == 0)
{
[alertView dismissWithClickedButtonIndex:0 animated:YES];
UIActionSheet *actionSheet = ...
[actionSheet showFromTabBar:self.tabBarController.tabBar];
}

How to check alertview values

This is my code :
self.myAlert = [[[UIAlertView alloc] initWithTitle:#"MNB" message:#"R u want to delete" delegate:self cancelButtonTitle:#"OK",nil otherButtonTitles:#"Cancel",nil] autorelease];
[myAlert show];
Here I would like to process if OK button click and also for cancel button, I would like to redirect the page if OK button clicked....I need the coding, IF condition statement when OK button clicked.....pls help me....
Read UIAlertViewDelegate Protocol Reference.
You can implement following methods.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
Just need to write the Delegate method of UIAlertView like this
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex==1){
NSLog(#"Cancelled Clicked");
}
if(buttonIndex==0){
NSLog(#"O.K Clicked");
}
}
Will surely work :)
You can use UIAlertView delegate methods. For example
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0) //your OK button
{
//Some code here
}
else if(buttonIndex == 1)
{
//Some other code
}
}

- (void)alertViewCancel:(UIAlertView *)alertView is not called

I've got the problem that the UIAlertViewDelegate method - (void)alertViewCancel:(UIAlertView *)alertView is not called when I cancel a AlertView with it's cancel button.
Weird is that the delegate method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex works perfectly.
Does anyone have an idea?
Thanks in advance
Sean
- (void)alertViewCancel:(UIAlertView *)alertView
{
if(![self aBooleanMethod])
{
exit(0);
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//some code
}
I call this when a button is clicked:
- (void)ImagePickDone
{
UIAlertView *alertDone = [[UIAlertView alloc]
initWithTitle:#"Done"
message:#"Are u sure?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles: #"Yes", nil];
[alertDone show];
[alertDone release];
}
The alertViewCancel is used for when the system dismisses your alert view, not when the user presses the "Cancel" button. Quote from apple docs:
Optionally, you can implement the
alertViewCancel: method to take the
appropriate action when the system
cancels your alert view. If the
delegate does not implement this
method, the default behavior is to
simulate the user clicking the cancel
button and closing the view.
If you want to capture when the user presses the "Cancel" button you should use the clickedButtonAtIndex method and check that the index corresponds to the index for the cancel button. To obtain this index use:
index = alertDone.cancelButtonIndex;
You can handle the Cancel at the index 0 of this delegate:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0){
//cancel button clicked. Do something here.
}
else{
//other button indexes clicked
}
}
This can be improved in two ways. First, it only handles the case that the user actually clicked a button. It doesn't handle the situation that [myAlert dismissWithClickedButtonIndex:] is called, or that the alert is dismissed in some other way. Second, button 0 is not necessarily the cancel button. In an alert with two buttons, the left one is at index 0, and the right one is at index 1. If you changed the titles so that the right button says "Cancel", then button 1 is logically the Cancel button. Instead of "willDismiss" you can implement "didDismiss" which will be called after the dialog has disappeared and not before.
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == alertView.cancelButtonIndex)
{
//cancel button clicked. Do something here.
}
else
{
//other button indexes clicked
}
}