OphirRam8
OphirRam8

Reputation: 9

Is there a way to navigate to other views using a UIActionSheet?

Is there is a way to use an Action sheet to navigate to another view?

I have a button - "Go" and an Action sheet that asks "Do you want to proceed?" and in it "Yes" and "Cancel"

Can I can navigate to another view when pressing yes?

Upvotes: 0

Views: 648

Answers (2)

Sierra Alpha
Sierra Alpha

Reputation: 3727

Yes, it is possible. To do this, the viewController that represents the UIActionSheet needs to adopt UIActionSheetDelegate. Upon dismissing the action sheet with either Yes or Cancel, - actionSheet:didDismissWithButtonIndex: method gets called, and from there you can navigate to another view or just ignore it.

References: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html#//apple_ref/occ/cl/UIActionSheet

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIModalViewDelegate_Protocol/UIActionSheetDelegate/UIActionSheetDelegate.html


Edit:

@interface MyViewController : UIViewController <UIActionSheetDelegate>
-(IBAction)showActionSheet:(id)sender;
@end


@implementation MyViewController

-(IBAction)showActionSheet:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Do you want to procced?"  delegate:self  cancelButtonTitle:@"Cancel"  destructiveButtonTitle:@"YES"  otherButtonTitles:nil];

    [actionSheet showInView:self.view];
}

-(void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    switch (buttonIndex) {
        case 0: { 
            // for illustration
            // let's assume (1) you have a navigation controller
            // (2) you are using storyboard
            // (3) in the storyboard, you have a viewController with identifier MyChildViewControllerIdentifier
            MyChildViewController *mcvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MyChildViewControllerIdentifier"];
            [self.navigationController pushViewController:mcvc animated:YES];
            break;
        }
         default:
            break;
     }
 }

P.S. I didn't run it, if there is any error, let me know to fix it.

Upvotes: 3

Dismiss the actionsheet and then [self presentmodalviewcontroller:myview animated:yes]

Upvotes: 1

Related Questions