Chaus
Chaus

Reputation: 77

Changing view controllers without the storyboard

I am trying to change views but I want have to do it through code and not through storyboard for reasons that don't really matter, and I found this code:

[[self ] presentModalViewController:[self destinationViewController] animated:NO];

the problem is I don't know what to put in for presentModalViewController or destinationViewController. Is there some way to figure out what my views are called so i can put them in?

Upvotes: 1

Views: 427

Answers (2)

What what
What what

Reputation: 527

At the top you should have

#import "NameOfViewController.h"

Then later

UIViewController *destinationViewController = [[NameOfViewController alloc] init];
[self presentViewController:destinationViewController animated:NO completion:nil];

Use presentViewController instead of presentModalViewController as the latter method is deprecated. I hope that works for you.

Upvotes: 2

SwiftArchitect
SwiftArchitect

Reputation: 48524

In the example you give, [self destinationViewController] creates and returns a UIVIewController.

You can either:

  1. load one from a NIB file

    controller = [[MyController alloc] initWithNibName:@"nib-resource-name" bundle:nil];

  2. load one from the storyboard

    MyController * controller = [self.storyboard instantiateViewControllerWithIdentifier:@"controller-storyboard-id"];

  3. create one programmatically

    How do I create a UIViewController programmatically?

Upvotes: 0

Related Questions