21

Edit: When solving this problem, I found that it is much easier to start with your UITabBarController, then perform login validation via your AppDelegate.m's didFinishLaunchingWithOptions: method.

Question: This code is in the the application didFinishLaunchingWithOptions: method in my AppDelegate.m

if([result isEqualToString: @"log"])
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"TabBarControl"];
    [(UINavigationController*)self.window.rootViewController pushViewController:ivc animated:NO];
    NSLog(@"It's hitting log");
}

It simply takes an HTTP response for the user being logged in, and takes them to my TabBarController. The problem is that it's using a push rather than a modal transition to display the page. Since presentModalViewController method is deprecated or deleted in iOS7, how can I programmatically force a modal presentation?

EDIT: )

Chisx
  • 1,886
  • 4
  • 22
  • 48
  • 3
    All you need to do is look in the reference docs for this method and it will show you what to use in its place. – rmaddy Jan 02 '14 at 23:24
  • 1
    You should not use segues to go backwards (other than an unwind segue) to previous controllers, which it looks like you're doing in this image. This will cause new controllers to be instantiated, not go back to the one you started with. – rdelmar Jan 02 '14 at 23:36
  • The system knows that you've accepted an answer to the question and displays that information when it's relevant. Please [don't add "solved" or the like to your title](http://meta.stackexchange.com/questions/116101/is-it-ok-to-add-solved-to-the-title-of-a-question). – jscs Jan 26 '14 at 05:07
  • I'm sorry I was not aware of these rules. I am very new to the cite, as you can see. I'd prefer this question be deleted upon the following basis: 1. The problem is being approached incorrectly 2. I failed to search before asking the question(refer 5th comment under answer) – Chisx Jan 26 '14 at 05:45

5 Answers5

31

The old presentViewController:animated: method has been deprecated, we need to use presentViewController:animated:completion instead. You now simply have to add a completion parameter to the method - this should work:

if([result isEqualToString: @"log"])
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"TabBarControl"];
    [(UINavigationController*)self.window.rootViewController presentViewController:ivc animated:NO completion:nil];
    NSLog(@"It's hitting log");
}

The docs are a good place to start - the docs for UIViewController presentViewController:animated tell you exactly what you need to know:

presentModalViewController:animated:

Presents a modal view managed by the given view controller to the user. (Deprecated in iOS 6.0. Use presentViewController:animated:completion: instead.)

- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
Community
  • 1
  • 1
Undo
  • 25,204
  • 37
  • 102
  • 124
  • Ok, thanks so much, this has helped me understand a lot. I'm having a problem using the other logic for just starting with the login page, that' why I posted the picture – Chisx Jan 02 '14 at 23:32
  • If you're having different problems, you can always (and should) ask more questions. Thanks for confirming it works! – Undo Jan 02 '14 at 23:35
  • Lol I'm discouraged to ask another question(b/c of my down votes), but do you mean on this question or another? – Chisx Jan 02 '14 at 23:36
  • 3
    @WhiteHatPrince If you have another problem, you would ask another question. Also, to avoid downvotes, make sure you (1) search before asking, and (2) include a great explanation with a simple code sample. In this case, I believe you didn't search first. Questions much like this have been asked and answered before. – Undo Jan 02 '14 at 23:37
  • No problem, glad to help! – Undo Jan 02 '14 at 23:39
  • Ah ok. I see that this question is around already now, but I was actually trying to simplify a more complicated question that no one has been able to help/answer me on. Rather than asking another question, I think you would know this. If you take a look at the pic that I added in the EDIT, *question:* should I be starting the entire app with the tabBar and then conditionally presenting my login page, or can I start with my login page, and conditionally present my tabBar?( – Chisx Jan 03 '14 at 00:01
  • @WhiteHatPrince, it would be better to make the tab bar controller the root view controller of the window, and present your login controller from the viewDidAppear method (without animation) of the controller in the first tab. – rdelmar Jan 03 '14 at 00:25
  • Thanks so much @rdelmar , so this is also **not** a job for AppDelegate is what you're saying? – Chisx Jan 03 '14 at 00:33
  • @WhiteHatPrince, I wouldn't use the app delegate. I would put the logic to determine login status in the first controller of the tab bar controller. – rdelmar Jan 03 '14 at 00:35
  • @rdelmar Thanks so much.. I've been confused about this for a very long time. – Chisx Jan 03 '14 at 03:03
27

Swift

Since the accepted answer is in Objective-C, this is how you would do it in Swift. Unlike the accepted answer, though, this answer does not reference the navigation controller.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "secondViewController") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)

Change the storyboard name, view controller, and id as needed.

See also how to dismiss a view controller programmatically.

Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198
2

In swift 4.2 you can do it like this. For those who want this answer in swift updated version.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ExampleViewController")
self.present(controller, animated: true, completion: nil)
Shahzaib Maqbool
  • 1,331
  • 13
  • 22
0

In Swift 3/4

     let storyB = UIStoryboard(name: "Main", bundle: nil) 
     let secondViewController = storyB.instantiateViewController(withIdentifier: "SecondViewControllerID") as! SecondViewController
     self.present(secondViewController, animated: true, completion: nil)
Sh_Khan
  • 86,695
  • 6
  • 38
  • 57
0
 let storyB = UIStoryboard(name: "Main", bundle: nil) 
 let secondViewController = 
 storyB.instantiateViewController(withIdentifier: 
 "SecondViewControllerID") as! SecondViewController
 self.present(secondViewController, animated: true, completion: nil)

In the secondViewController use this code to go back.

 self.dismiss(animated: true, completion: nil)