2

I really struct for the simple problem.

I am simply moving from one view controller to another view controller using the following code.

homescreen *home;
home=[self.storyboard instantiateViewControllerWithIdentifier:@"home"];
[self presentViewController:home animated:YES completion:nil];

Same code tried using in AppDelegate method.

UIStoryboard *storyboard;
sur= [storyboard instantiateViewControllerWithIdentifier:@"survey"];
[self.window.rootViewController presentViewController:sur animated:YES completion:nil];

When I am using this code I get this crash:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target

i don't want to make it as root view controller ,simply i need to move it.

Snazzy Sanoj
  • 804
  • 1
  • 11
  • 28
Kishore Kumar
  • 4,265
  • 3
  • 26
  • 47

5 Answers5

3

You have only declared UIStoryboard *storyboard not initialised it, and in next line you are calling method instantiateViewControllerWithIdentifier upon nil object. Eventually storyboard variable replaces nil in following line and crashes.

sur= [storyboard instantiateViewControllerWithIdentifier:@"survey"];
Aamir
  • 16,329
  • 10
  • 59
  • 65
3

You cannot present view directly on empty window.rootViewController. You need to set it to any viewController first and then present your viewController that need to be show. Refer to this code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    InitialViewController *initialVC = [storyboard instantiateViewControllerWithIdentifier:@"InitialViewController"];
    self.window.rootViewController = initialVC;
    [self.window makeKeyAndVisible];

    ShowThisViewController *showThisVC = [storyboard instantiateViewControllerWithIdentifier:@"ShowThisViewController"];
    [self.window.rootViewController presentViewController:showThisVC animated:YES completion:nil];

    return YES; }
muazhud
  • 914
  • 7
  • 18
1

Visit this.

I think Aamir is right. But, in addition, you need to make your window visible.

You need to call

[self.window makeKeyAndVisible];

before presenting any ViewController.

Community
  • 1
  • 1
CloudTuan
  • 91
  • 3
1

Use This code

    DEMORootViewController *VerifyV = [self.storyboard  instantiateViewControllerWithIdentifier:@"Identifier"];  

   UINavigationController *navigationController=[[UINavigationController alloc]initWithRootViewController:VerifyV];

    self.window.rootViewController = navigationController;
Mahesh reddy
  • 134
  • 13
0

Simply try this code:

UIStoryboard *storyboard;
survey *sur= [storyboard instantiateViewControllerWithIdentifier:@"survey"];
UIViewController *curViewCont = [UIApplication sharedApplication].keyWindow.rootViewController;
while (curViewCont.presentedViewController) {
    curViewCont = curViewCont.presentedViewController;
}
[curViewCont presentViewController:sur animated:YES completion:nil];

Hope it will work for you.

Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70