Question

After the user clicks a button I want an action sheet to come up asking for them to choose between two options. Once they have selected an option, I want an AlertView to come up telling them they will leave the application and have them choose to cancel to operation or continue to the other application.

Code is as follows:

.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface mapView : UIViewController <MKMapViewDelegate, UIActionSheetDelegate, UIAlertViewDelegate>

@property (nonatomic, weak)IBOutlet MKMapView *mapView;
@property (nonatomic, weak)IBOutlet UIBarButtonItem *getDirections;

- (IBAction)selectDestination:(id)sender;
- (void)checkLeave;

@end

.m

- (IBAction)selectDestination:(id)sender
{
    UIActionSheet *selectDestinationAS = [[UIActionSheet alloc] initWithTitle:@"Select Destination: " delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Destination 1", @"Destination 2", nil];
    [selectDestinationAS showInView:self.view];
}

- (void)checkLeave
{
    UIAlertView *checkLeaveAlert = [[UIAlertView alloc] initWithTitle:@"Leave CDSI?" message:@"This will open the Maps application to continue directions. Are you sure you want to continue?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Open", nil];
    [checkLeaveAlert show];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self checkLeave];

    if ([[actionSheet buttonTitleAtIndex:buttonIndex]  isEqualToString: @"Destination 1"]) {
        NSURL *BOHMDirections = [NSURL URLWithString:@"http://maps.apple.com/?daddr=Destination1&saddr=Current+Location"];
        [[UIApplication sharedApplication] openURL:BOHMDirections];
    } else if ([[actionSheet buttonTitleAtIndex:buttonIndex]  isEqualToString: @"Destination 2"]) {
        NSURL *BOPDirections = [NSURL URLWithString:@"http://maps.apple.com/?daddr=Destination2&saddr=Current+Location"];
        [[UIApplication sharedApplication] openURL:BOPDirections];
    }
}

The ActionSheet shows up, when an option is selected the Maps app opens (as desired) but the AlertView shows up only after you reenter the original app. How do I get it to show up before I leave the app?

Was it helpful?

Solution

Navigate to the external app on UIAlertView delegate.

To pass the selected item index at UIActionSheet, pass the index as parameter in checkLeave method and set as tag to the UIAlertView

By this way, the UI execution will be, on ActionSheet Clicked, the alertview ask confirmation with user. Once the user confirms, the navigation will be performed based on the action sheet selection. To hold, the action sheet selection, we are passing that data as tag.

If you need, you can add a private property to hold the item data clicked and access it in UIAlertViewDelegate.

- (void)checkLeave :(NSInteger)index
{
    UIAlertView *checkLeaveAlert = [[UIAlertView alloc] initWithTitle:@"Leave CDSI?" message:@"This will open the Maps application to continue directions. Are you sure you want to continue?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Open", nil];
    [checkLeaveAlert setTag:index];
    [checkLeaveAlert show];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self checkLeave : buttonIndex];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   if(alertView.tag == 1){
       //Destination 1 clicked
  }
}

OTHER TIPS

Similar to the action sheet, UIAlertView has a delegate protocol that you should implement/comply with. Especially the method alertView:didDismissWithButtonIndex: is of interest.

Call the Maps app there and not in clickedButtonAtIndex

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top