0

I wanna present a UIViewController when come into foreground app.
And i am using this code in applicationWillEnterForeground method in Objective C appDelegate app , but it does not works for me.

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
PasscodeViewController *vc = [sb instantiateViewControllerWithIdentifier:@"passcodeVCID"];
[self presentViewController:vc animated:YES completion:nil];
reza_khalafi
  • 6,230
  • 7
  • 56
  • 82
  • What do you reckon is `self` inside appDelegate? – NSNoob Nov 08 '16 at 13:48
  • i used to using SELF in uiviewcontroller. there is works. but i dont know how to handle it here. – reza_khalafi Nov 08 '16 at 14:30
  • That's because in UIViewController class, self is a ViewController. `presentViewController` is a method defined in UIViewController.h so it works there. In AppDelegate, self is AppDelegate and it has no method called `presentViewController`. – NSNoob Nov 08 '16 at 14:33
  • You need to access your currently visible VC first and then use that instance to call the presentViewController method. See [this answer](http://stackoverflow.com/a/39416067/4029561) to learn how can you get current visible VC. – NSNoob Nov 08 '16 at 14:36

1 Answers1

0
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
PasscodeViewController *vc = [sb instantiateViewControllerWithIdentifier:@"passcodeVCID"];

// Need to present with rootviewcontroller

[self.window.rootViewController presentViewController:vc animated:YES completion:nil];
NSNoob
  • 5,548
  • 6
  • 41
  • 54
Bucket
  • 449
  • 3
  • 20