1

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];

 }
Bachzen
  • 323
  • 2
  • 8
  • 22

3 Answers3

1

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]
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
0

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

icodesign
  • 876
  • 1
  • 6
  • 12
0

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;
Community
  • 1
  • 1
adamF
  • 961
  • 2
  • 9
  • 23
  • If you want to push another view onto a navigationController that already exists (and mainVC is already on its viewController stack), you need to remove your property and instance variables for navigationController and use self.navigationController (which is set automatically by your navigationController, and hidden by your new variable with the same name) to access your existing UINavigationController. After you've removed your navigationController property, call `[self.navigationController pushViewController:myViewController animated:YES];` – adamF Oct 25 '13 at 17:49
  • Please either accept this answer if it worked, or post more detail and code showing what exactly isn't working – adamF Oct 25 '13 at 21:08