Search code examples
iphonexcodeuiactionsheetuistoryboard

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

}
}

Solution

  • 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.