1

I'm developing an app in which I need to show a ViewController when I press the "OK" button in an UIAlertView. How I can do that?

I found on here this solution: iOS Dev: Segue by pressing a button in an Alert?, but it didn't work.

xCode says:

the method: presentModalViewController is deprecated

and I found on the web that I should use the method presentViewController.

I tried to do that but it shows me a black screen without any interaction, what's wrong? I paste here my code, maybe you can help me.

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != alertView.cancelButtonIndex)
    {
        PromoViewController *pvc = [[PromoViewController alloc] init];
        [self presentViewController:pvc animated:YES completion:nil];
    }
}

Thank you

Community
  • 1
  • 1
lucgian84
  • 833
  • 4
  • 11
  • 29

3 Answers3

2

In order to use a segue programatically you need to called performSegueWithIdentifier.

First make sure your segue has an identifier in the storyboard.

Then use this code instead...

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != alertView.cancelButtonIndex)
    {
        [self performSegueWithIdentifier:@"YourSegueIdentifier"];
    }
}

This will do what you want.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • I made so: 1. In storyboard I connect the button with the new ViewController I've to load 2. In the code I inserted your solution But when I press on the button I connected to the new ViewController it shows me the new ViewController first and then the UIAlertView – lucgian84 Jul 22 '13 at 13:04
0

See this: Make button it UIAlertView perform Segue

It will show you how to create a manual segue and call it.

Community
  • 1
  • 1
jimejim
  • 594
  • 6
  • 8
-1

Well another approach is to get the instance and do it programatically as

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                         bundle: nil];
PromoViewController *controller = (PromoViewController*)[mainStoryboard 
                    instantiateViewControllerWithIdentifier: @"<Controller ID>"];
[self presentViewController:controller animated:YES completion:nil];

Note controller ID is the storyboard id you set for the scene via attribute inspector

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • 1
    Yeah, it may work but you're completely missing the point of segues. ::Facepalm:: – Fogmeister Jul 22 '13 at 13:14
  • @Fogmeister : not completly missing the point.this is also right.I said in the answer explicitely, "Another approch" .If you miss that.And it is never wrong calling an instance – Lithu T.V Jul 22 '13 at 13:18