1

What i am trying to achieve is go to another view when my alert view button is clicked. My alert view is inside my loadingView, this alert view is called from another class called classA.

This is how it is called in classA.

[LoadingViewController showError];

This is the method in loadingView in loadingView class.

+ (void)showDestinationError{
UIAlertView *alert = [[UIAlertView alloc] 
                      initWithTitle:@"Error" 
                      message:@"Error"
                      delegate:self 
                      cancelButtonTitle:@"OK" 
                      otherButtonTitles: nil];
alert.tag = DEST_ERR;
[alert show];
}

Button action

+ (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag = DEST_ERR){

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
    UINavigationController *secondView = [storyboard instantiateViewControllerWithIdentifier:@"NavigationController"];
    secondView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;    
    [secondView presentModalViewController:secondView animated:YES];
}
}

This gives me an error. 'Application tried to present modal view controller on itself. Presenting controller is UINavigationController:..

Note: my method is a '+'

Hexark
  • 413
  • 6
  • 22

1 Answers1

5

The problem is this line:

[secondView presentModalViewController:secondView animated:YES];

A view controller can't present itself, and in this case doesn't even exist yet).

The most obvious way to fix this is to make your clickedButtonAtIndex an instance method since it needs to access information about the particular instance. You would then use this:

[self presentModalViewController:secondView animated:YES];

Otherwise, you need to get a reference to a view which can present your view controller. There are various ways to do this depending on how your app is setup which could include getting it from your app delegate or referencing the app window.

lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • yeah, but self cannot because my alertview method is a `+` `no known class method for selector presentmodalviewcontroller animated` – Hexark Apr 25 '12 at 07:52
  • How do I get a reference? Thanks! – Hexark Apr 26 '12 at 07:16
  • Well, you have to store a reference to the view that you want to present it somewhere. Perhaps the AppDelegate or a Singleton. This is really what an instance method is designed for though so you should just make it a "-" method instead of a "+" if you want to present it from self! – lnafziger Apr 26 '12 at 17:04