Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I load another view controller from the current view controller's implementation file?

I need to create an app which features a login/signup form and then a custom Google Map. I am new to iOS programming and trying to learn the things needed for this app very fast.

So, I have created the frontend and the backend of the login form, it works. I have an action which is triggered by the "Log in" button which verifies the credentials and either trigger a error either presents the Google Map.

I want to show that Google Map in another view controller which controls another xib file. I created the view controller and the xib file.

My question is how can I load another view controller from an action placed in the implementation file of the current view controller?

Now, I have this code:

UIViewController *mapViewController =
                [[BSTGMapViewController alloc] initWithNibName:@"BSTGMapViewController"
                                                           bundle:nil];

How can I make it the "root view controller" of the window and maybe featuring a transition (considering my logic is ok)?

like image 998
Octavian Avatar asked Mar 17 '13 17:03

Octavian


People also ask

How do I open a new view controller?

To create a new view controller, select File->New->File and select a Cocoa Touch Class. Choose whether to create it with Swift or Objective-C and inherit from UIViewController . Don't create it with a xib (a separate Interface Builder file), as you will most likely add it to an existing storyboard.


1 Answers

If you want to open the ViewController from another one then you should define it like this in your IBAction. It is good idea to make the viewController as property though.

FirstViewController.h

@class SecondViewController;

@interface FirstViewController : UIViewController

@property(strong,nonatomic)SecondViewController *secondViewController;
@end

FirstViewController.m

-(IBAction)buttonClicked:(id)sender{
    self.secondViewController =
                    [[SecondViewController alloc] initWithNibName:@"SecondViewController"
                                                               bundle:nil];
   [self presentViewController:self.secondViewController animated:YES completion:nil]; 

}

You should make a viewController as rootViewController in your AppDelegate class something like this

YourFirstViewController *firstViewController=[[YourFirstViewController alloc]initWithNibName:@"YourFirstViewController" bundle:nil];

self.window.rootViewController=yourFirstViewController;
like image 117
nsgulliver Avatar answered Sep 30 '22 11:09

nsgulliver