How do I change initwithNibName in storyboard?
The UIStoryboard
class is your friend:
UIStoryboard* sb = [UIStoryboard storyboardWithName:@"mystoryboard"
bundle:nil];
UIViewController* vc = [sb instantiateViewControllerWithIdentifier:@"ExampleViewController"];
initWithNibName method in storyboard
I just ran into this problem myself. Storyboards appear to not use initWithNibName:bundle:
, but either initWithCoder:(NSCoder *)aDecoder
or initWithStyle:(UITableViewStyle)style
.
The default implementations of the two methods look like:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// Custom initialization
}
return self;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
I've yet to use the initWithStyle version myself, but I'd imagine it's called for a UITableViewController. If in doubt, you could simply add both methods to the file, along with an NSLog() call that prints the method's name (or any other unique string). You can then run it in the simulator to see which is called, and delete the other.
I'd strongly recommend against calling initWithNibName:bundle:
yourself from any of the other init
methods. Better to just move the code to the correct method.
initWithNibName:@ -VS- self.storyboard instantiateViewControllerWithIdentifier:@?
initWithNibName
is used when your viewController is created with a .xib, and when you handle your views that way. Even then, you should probably avoid using this method unless absolutely necessary.
On the other hand instantiateViewControllerWithIdentifier
is the preferred way of creating a new viewController when using storyboards, and the previous way will not work in that case.
Changing initWithNibName to storyboardWithName
If you simply want to load the initial view controller of the storyboard when the app launches, just return YES
in application:didFinishLaunchingWithOptions:
.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}
If you want to load a specific controller from the storyboard, you need to first get the storybard instance by
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];
then use it to instantiate the controller you need
UIViewController * controller = [storyboard instantiateViewControllerWithIdentifier:@"controllerIdentifier"];
where controllerIdentifier
has been assigned as storyboard identifier to the controller in Interface Builder.
Here's an example loading a specific view controller, presenting it at launch.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];
UIViewController * controller = [storyboard instantiateViewControllerWithIdentifier:@"controllerIdentifier"];
self.window.rootViewController = controller;
return YES;
}
How to Change Root View from anywhere in appliction in iOS
To change the root view of the application you can do it in this way
SignUpVC *main = [[SignUpVC alloc]initWithNibName:@"SignUpVC" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:main];
[[UIApplication sharedApplication].keyWindow setRootViewController:navController];
customize initWithNibName
It makes perfectly sense to me. This is the way you would override an initializer to add some custom initialization in Objective-C. What do you think is wrong with it ?
Custom initialisation while using storyboard
You wouldn't do this with storyboards.
The way to do it is to use properties to give the correct values once the view controller has been initialised.
This is done in - (void)prepareForSegue:(UIStoryBoardSegue *)segue sender:(id)sender
.
You can access segue.destinationViewController
and then use this to put values in to.
Xcode Storyboard and xib connection
You can create XIB
when you create connector.h
and connector.m
by selecting it subclass of UIViewController
and click on the checkbox for: "With XIB for User Interface". If you have created already .m & .h files then you can just add a new GUI file by selecting View from the window & finally setting its Controller Custom class to connector
You could have StoryBoard
and XIB
together in the same project. See for more help.
For presenting the view Controller you could use the following code
YourViewController *viewController=[[YourViewController alloc]initWithNibName:@"ViewControllerName" bundle:nil];
[self presentViewController:viewController animated:YES completion:nil];
In case of NavigatinController
[self.navigationController pushViewController:viewController animated:YES];
Related Topics
Repeat Interval for Unnotification
Retrieve User Defaults Information from an Apple Watch
Copy Text to Clipboard with iOS
Drawrect Circle and Animate Size/Color
Bringing a Subview to Be in Front of All Other Views
Does iOS Provide Built in Text to Speech Support or Any Class Like Nsspeechrecognizer
Uitextview Disabling Text Selection
How to Expand Uicollectionview Contentsize When Paging Enable
How to Extract a Url from a Sentence That Is in a Nsstring
"File Not Found", "Linker Command Failed with Exit Code 1" in Xcode 4.5.1
Why My Nsdateformatter Returns Null
iOS - Cydia Open Command and Its Counterpart
Get Uiscrollview to Scroll to the Top
Pod Install Displaying Error in Cocoapods Version 1.0.0.Beta.1
How to Get the Size of a Scaled Uiimage in Uiimageview
Status Bar Showing Black Text, Only on iPhone 6 iOS 8 Simulator