Call NavigationController (xib) from ViewControlle

2019-09-07 17:19发布

I'm normally just use storyboard for my iOS, but needed to integrate an SDK which does not use storyboard, rather xib. My initial view controller is from storyboard, and when a button is pushed (IBAction), I would like it to go to the xib view controller, but not sure how programmatically to do so. Here is my AppDelegate:

 //AppDelegate.m

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:...{   

// set to storyboard on launch
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"iPhoneStoryboard"
                                                         bundle: nil];

UIViewController* viewController = [mainStoryboard instantiateInitialViewController];
[self.window setRootViewController:viewController];

 [window makeKeyAndVisible];
 return YES;
 }

Here is my code .h:

 //mainVC.h
 #import <UIKit/UIKit.h>

 @interface MainVC : UIViewController{
 UIWindow *window;
 UINavigationController *navigationController;
 }

 @property (nonatomic, retain) IBOutlet UIWindow *window;
 @property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
 @property (nonatomic, retain) IBOutlet UIButton *buttonScan;

 -(IBAction) ActionScan;

 @end

And the .m:

 //mainVC.m
 @interface MainVC ()

 @end

 @implementation MainVC

 @synthesize window;
 @synthesize navigationController;
 @synthesize buttonScan;

 - (IBAction)ActionScan{

window.rootViewController = navigationController;
[window makeKeyAndVisible];

 }

3条回答
forever°为你锁心
2楼-- · 2019-09-07 17:56

create instance of your xib viewController with initWithNibName

MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];

& then push this controller instance on navigation controller

[self.navigationController pushViewController:desController animated:YES]
查看更多
趁早两清
3楼-- · 2019-09-07 18:03

In the view controller in storyboard, code below:

  1. alloc & init destination view controller:

    UIViewController *desController = [[SomeViewController alloc]init];

  2. show the destination view controller:

    [self.navigationController pushViewController:desController animated:YES]

    or

    [self.navigationController presentViewController:desController animated:YES completion:nil

查看更多
Viruses.
4楼-- · 2019-09-07 18:03

Usually you don't set the window's rootViewController directly. Instead you should use

 - (IBAction)ActionScan{
    [self presentViewController:self.navigationController animated:YES completion:nil]; 
 }

to show your next view controller. The first and accepted answer to this question gives a more detailed explanation of how this works.

Changing root view controller of a iOS Window

Setting window.rootViewController and calling [window makeKeyAndVisible] usually happens in your app delegate, but since you're using storyboards, it is done for you under the hood.

Also, unless you're using multiple windows, you can access your UIWindow from anywhere using your app delegate singleton object.

MyAppDelegateClass *appDelegate = (MyAppDelegateClass*)[[UIApplication sharedApplication] delegate];
UIWindow *mainWindow = appDelegate.window;
查看更多
登录 后发表回答