It has been a while since I created a project from scratch. Back then in XCode 4 / 5 the developer was given the choice between storyboard or Xib to get the project started. In Xcode 6 though every template is forced to get started as StoryBoard.
I have removed the story board. Then I have opened info.plist in Supporting files and set Main storyboard file base name : Main to Main storyboard file base name: <blank>.
Then I created a basic testViewController with Xib (same name).
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
testViewController *firstViewController = [[testViewController alloc] initWithNibName:nil bundle:nil];
self.navigationController.viewControllers = [NSArray arrayWithObject:firstViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
And this still shows me a black view controller. I checked the xib file, the File owner is set to the view controller and the view delegate is also set.
What am I missing please?
change these two things:
#property (nonatomic, retain) IBOutlet UIWindow *window;
testViewController *firstViewController = [[testViewController alloc] initWithNibName:nil bundle:nil];
To this, and add this code, with the real name or you xib file:
#property (nonatomic, retain) UIWindow *window; // Are you using ARC?
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// I think your xib files is testViewcontroller.xib, if not change it.
testViewController *firstViewController = [[testViewController alloc] initWithNibName:testViewController.xib bundle:nil];
// change this also
self.navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
Related
thank you for taking time to answer me !
I'm inexperienced in IOS development, after several searches on google, I turn to you.
My problem is that : I will wish to use UINavigationController, with new main view, to allow me to create a back button.
Here is my AppDelegate.m :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MainViewController *mvc = [[MainViewController alloc]init];
//self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
//UINavigationController * navController = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = mvc;
[self.window makeKeyAndVisible];
return YES;
}
and my AppDelegate.h :
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
//#property (strong, nonatomic) ViewController *viewController;
#end
During development I changed the main view of the application as you can see above the two lines commented out.
I would like to use "UINavigationController" with this new main view, what does it change? and where?
Thank you and sorry if i'm not clear ! (Google Translate)
Set your MainViewController *mvc as the root of your navController and make it the root view controller of the window.
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController: mvc];
self.window.rootViewController = navController;
I have SingleView application. Now I want to add a mainwindow to my project.
How can I add the window into my project?
Thanks in advance.
First Add Your LoginViewController as self.window.rootViewController such like
Add this code in your Appdelegate.h file
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#interface AppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
}
#property (nonatomic, retain) UIWindow *window;
#end
Add this code in your Appdelegate.m file
(Here i also added UINavigationController too)
#synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
LoginViewController *loginViewController = [[LoginViewController alloc] init];
UINavigationController *loginNVController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
loginNVController.navigationBarHidden = YES;
self.window.rootViewController = loginNVController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
OR Add Directly
Check this link for adding window.xib .
Check this link
If you have choosen single view application. You can give the xib name as per the view. That is more general in sense. If you have selected empty application then go to you xcode project and add new file. which extends from UIViewController.
AppDelegate can not be referenced in any .xib files. the main method will instantiate AppDelegate object and AppDelegate object will then instance other view controllers.
For more you can read some apple documentation.
Thanks
Rinku
I'm very new to ios developing and many of the tutorials i watch follow the xib approach since they were recorded before XCode 5. How can i go with the xib approach in Single View Application? When i delete storyboard and select one of the xib's as the main interface it's not working. Thanks for any tip.
add new file in that select the cococatouch and select the Objective -C class and then go to the next click.
you show the below screen
at the bottom must select the with xib for user Interface.and next
AppDelegate.h:
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
ViewController *viewObj;
UINavigationController *navObj;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewObj;
#property (strong, nonatomic) UINavigationController *navObj;
AppDelegate.m:
#synthesize viewObj,window,navObj;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
viewObj = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
navObj = [[UINavigationController alloc] initWithRootViewController:viewObj];
window.rootViewController=navObj;
[window makeKeyAndVisible];
return YES;
}
Select Empty application template while creating new project.
Select Cocoa touch from left pane -> objective-c class -> then give name of viewController and tick option of Xib user interface.
and then in apply below code in appDelegate file:
appDelegate.h File:
#import <UIKit/UIKit.h>
#import "HomeViewController.h"
#interface HomeAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong,nonatomic)HomeViewController *homeViewController;
#property (strong,nonatomic)UINavigationController *navigationController;
appDelegate.m File:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.homeViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.homeViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
#end
you can do it by following these steps:-
Create a new project
Select Single View Application
Set ProjectName and other settings
Save Project at location
Select Project in Navigator Panel in Left
Remove Main.storyboard file from project
Add New .xib file in Project
Set Main Interface to your .xib file in "General Tab" on right panel.
Paste following code in didFinishLaunchingWithOptions method
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
Courtesy:- MKR
https://stackoverflow.com/a/19633059/1865424
Just check it have you missed any of the step.
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];
}
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]
In the view controller in storyboard, code below:
alloc & init destination view controller:
UIViewController *desController = [[SomeViewController alloc]init];
show the destination view controller:
[self.navigationController pushViewController:desController animated:YES]
or
[self.navigationController presentViewController:desController animated:YES completion:nil
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;
I am having issues switching between view controllers.
My md360AppDelegate.h header looks like this
#import <UIKit/UIKit.h>
#class md360ViewController;
#interface md360AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) md360ViewController *viewController;
#property (strong, nonatomic) UINavigationController *navigationController;
#end
and my md360AppDelegate.m implementation looks like this.
#import "md360AppDelegate.h"
#import "md360ViewController.h"
#implementation md360AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[md360ViewController alloc] initWithNibName:#"md360ViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self.window setRootViewController:self.navigationController];
[self.window makeKeyAndVisible];
return YES;
}
#end
i am creating an instance of UINavigationController and storing it in navigationController property of this class.
I want to change the ViewController when a user click on a button in md360ViewController.
My md360ViewController.h looks like this.
#interface md360ViewController : UIViewController
#property IBOutlet UIButton *homePathwayBtn;
#property IBOutlet UIButton *homeDiseaseBtn;
#property IBOutlet UIButton *homePipelineBtn;
- (IBAction)homeButton:(id)sender;
#end
and my implementation looks something like
#import "md360ViewController.h"
#import "md360AppDelegate.h"
#import "pipeViewDisease.h"
#import <QuartzCore/QuartzCore.h>
- (IBAction)homeButton:(id)sender {
UIViewController *pipeViewDisease = [[UIViewController alloc] initWithNibName:#"pipeViewDiease" bundle:nil];
[self.navigationController pushViewController:pipeViewDisease animated:YES];
}
and when i click on UIButton the application crash with following message.
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'Could not load NIB in
bundle: 'NSBundle
(loaded)' with name 'pipeViewDiease''
what could be the issue?
Probably just a misspelled NIB name:
initWithNibName:#"pipeViewDiease"
should be:
initWithNibName:#"pipeViewDisease"
^
Unless it's a spelling mistake (should be pipeViewDisease), this error usually occurs when files are re-named outside of the xCode environment.
To fix this:
remove the file in question from the project
re-import the files to your project.