Making your custom view controller to be the root IOS - ios

I have a main view controller using a navigation controller and i was making my application over that. Now i want to add a welcome view controller for my app and make it show first instead of my main view controller. Is there any way to do it.
What I did was adding a view controller to my storyboard and added two classes of the same name then i made it my root view controller and unchecked the root view controller from the main view but it is not showing on the window. Please help me, that how can i make my welcome view to appear before the main view controller. Thanks

Inside Interface Builder, under the attributes section (looks like a small slider) about a quarter of the way down the list of settings, there is a section labeled "View Controller." The second item in that section is a checkbox "Is Initial View Controller," check that box and you should see the starting arrow of the story board move to the specified view controller and the app should launch to that page.
Hope this Helps.

You could do this programmatically by setting the app window's rootViewController. From the app delegate's application:didFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIScreen *mainScreen = [UIScreen mainScreen];
self.window = [[KTAppWindow alloc] initWithFrame:mainScreen.bounds];
[window makeKeyAndVisible];
window.rootViewController = [[WelcomeViewController alloc] init];
return YES;
}
Then you just need to set up your trigger to switch from the Welcome view to the main view. This could be scheduling an NSTimer, for example. Whatever the trigger is, once it occurs just change the window's rootViewController instance to your MainViewController.
I hope this helps.

It calls "Splash Screen" you just need to add a new ViewController in your Storyboard or if you don't have a Navigation Controller add.
In the Splash class, in the viewDidLoad method you need to put this:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
HomeViewController *vc = [sb instantiateViewControllerWithIdentifier:#"Home"];
[[[[UIApplication sharedApplication] delegate] window] setRootViewController:vc];
[self performSegueWithIdentifier:#"Home" sender:self];
here you can add conditions if you needed

Related

Creating a UINavigationController for the first UIViewController

It seems there are several approaches to creating a UINavigationController to be the very first controller. Perhaps the easiest way is to simply click the View Controller in Storyboard and embed it in a navigation controller. But I would like to know the best approach when doing this only in code.
You can subclass UINavigationController, import the first view controller, and in viewDidLoad alloc and init an instance, then add it as a childViewController. In Storyboard replace the default view controller with a navigation controller and set the class to your nav controller. Note that in previous versions of iOS it was not recommended to subclass UINavigationController.
Or you can create another UIViewController, alloc init the first view controller, then alloc init a UINavigationController with that view controller as the root, add the navigation controller as a child view controller of this view controller, and add the navigation controller's view as a subview of this view controller's view. Change the class of the view controller in Storyboard. This is an awkward setup though, because you create a view controller whose purpose is to add a nav controller but it's not a nav controller itself.
I've read about another approach which involves creating the UINavigationController in the AppDelegate. Perhaps there are even more solutions.
What is the most appropriate approach, working in the latest development environment, targeting iOS 8+?
If you're starting with a controller in the storyboard, you only need to add two lines in the app delegate to embed that controller in a navigation controller. If you want to do it in code, I think this is the simplest way,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.window.rootViewController];
self.window.rootViewController = nav;
return YES;
}
If you want to do it entirely in code, with no storyboard, then you need to create the window, the navigation controller, and its root view controller. You also need to click on the project icon in the files list, and in the "General" tab, delete the word "main" from the "Main Interface" pull down (that entry tells the system to start with a storyboard named "main.storyboard").
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *vc = [ViewController new]; // You need to create this controller's view in its loadView method
vc.title = #"Root View Controller";
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
return YES;
}

Change main viewController

I am using multiple viewcontrollers but need to change which who loads first. I have found some old tips but those are regarding .nib files and I belive those might not be up to date. Preferably I'd like to find some option in the menu's, as I frankly think this should be very simple.
Any ideas to a simple way of changing with viewController is loaded first?
Open the storyboard file
Display the document outline
Select the view controller that you want to be first
Under the "Attributes inspector", select "Is Initial View Controller"
You can programmatically load a view controller first through your AppDelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewContoller = <firstViewControllerIWantToLaunch>;
return YES;
}
This ensures that the view controller you want to load first is the root view controller, and will be the first view controller presented.
If you're using storyboard, select the view controller you'd like, and search for the "Is initial view controller" checkbox. I can't tell you where exactly it is, since I don't have Xcode open.
If you're using xibs, you need to change it in the AppDelegate applicationDidFinishLaunchingWithOptions method. (Refer to Bryan's answer for that.
If you want to switch between the two view controllers programmatically, Try doing Some thing Like this,
In your AppDelegate.m file in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions.. Method
just place this condition..
If(loadFirstView){
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
FirstViewController *menu = [navController.storyboard instantiateViewControllerWithIdentifier:#"FIrstVC"];
navController.viewControllers = [NSArray arrayWithObjects:menu, nil];
[self.window makeKeyAndVisible];
}else{
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
SecondViewController *menu = [navController.storyboard instantiateViewControllerWithIdentifier:#"SecondVC"];
navController.viewControllers = [NSArray arrayWithObjects:menu, nil];
[self.window makeKeyAndVisible];
}
Don't Forget to assign StoryboardIDentifiers "FirstVC" and "SecondVC" in your storyboard fileā€¦This should do it, It works Well for me
As already answered here, you need to select your project's main storyboard file, select the main view controller, go to attributes inspector and, check the checkbox title "Is Initial View Controller" to make main view controller loading as first view controller upon app launch.

Setting First view controller in App Delegate

I am developing an iOS app and using storyboards. In my storyboard, I had set a view controller as the initial view controller. Everything working fine.
Now I have to write some login in app delegate to decide which view controller to show at the beginning because this depends on how far the user is in the login process.
So, I removed the initial view controller mark from my storyboard and removed the storyboard setting from my plist file.
Now, in the app delegate I have this code -
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
UIViewController *initialViewController = [storyboard instantiateViewControllerWithIdentifier:[XYZUtils getStartScreenViewController]];
XYZStartScreenViewController *startScreenViewController = (XYZStartScreenViewController *)initialViewController;
[self.window addSubview:startScreenViewController.view];
[self.window setRootViewController:startScreenViewController];
[self.window setBackgroundColor:[UIColor whiteColor]];
[self.window makeKeyAndVisible];
This does not display the view controller - I am just getting a black screen and no error messages. On using breakpoints to walk through the above code, there is no error. The view controller is being instantiated but it is not getting displayed.
Am I missing something here?
Read this post..
It is better you make an empty view controller and mark that as initial view controller
And do all login process in that view controller's viewDidLoad method.
Linking a new viewcontroller to Storyboard?
For some reason, when you deselect the initial view controller setting in the storyboard, then your app will not get a UIWindow setup in didFinishLaunching.
So, what you should so is instantiate your own window there; just add this to the beginning of your application didFinishLaunchingWithOptions method:
UIWindow* window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
I think this solution is perfectly fine, but a "cleaner" one since you are using the storyboard, is having some sort of "LoginManagerViewController" as your initial view controller with the responsibility of handling where to do next based on how far the user is in the login process.

How to push a view with custom left pane in SplitViewController

My app has a splitview controller, and I when a cell is selected in the detail view, I want to push a view that replaces both the left and right panes.
The code below replaces only the right pane with a custom view.
SecondViewController *secondView = [[SecondViewController alloc]
initWithNibName:NSStringFromClass([SecondViewController class])
bundle:nil];
[[self navigationController] pushViewController:secondView animated:YES];
I would like to load in the left pane another table.
You will want to replace the splitviewcontroller view. What you are doing there is pushing onto whatever navigationcontroller you are on which I assume is your details or your navigation side of the split view controller.
Something like this will brute force it but you'd have to change it for what you need more:
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
[myDelegate.splitViewController.view addSubView secondView.view];
Or maybe try replacing the app delegate rootViewcontroller
myDelegate.window.rootViewController = secondView;
This is all assuming you have the correct properties but you will have to think of a solution that doesn't just push a view controller onto whatever self is in your above code snippet. I hope that helps

Navigation Controller is null

I have a split-view app that allows a user to select and display a thumbnail of a chosen image. I have placed a UIButton in the detailViewController using Interface Builder. When this button is pressed, I would like to have it change to a full screen view of the image. I have set up a new View Controller, called FullViewController and thought I had everything connected. The problem is that the navigation controller is null. I adjusted the AppDelegate.m to the following:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch.
// Set the split view controller as the window's root view controller and display.
self.window.rootViewController = self.splitViewController;
UINavigationController *nvcontrol =[[UINavigationController alloc] initWithRootViewController:fullViewController];
[window addSubview:nvcontrol.view];
[self.window makeKeyAndVisible];
return YES;
}
This is the function in the DetailViewController.m which is called when the button is pressed. The navigation controller comes up null in here.
//Function called when button is pressed - should bring up full screen view
- (IBAction) pressFullViewButtonFunction: (id) sender{
//viewLabel.text = #"Full View";
if (fullViewController == nil){
FullViewController *fullViewController = [[FullViewController alloc] initWithNibName:#"FullViewController" bundle:[NSBundle mainBundle]];
NSLog(#"fullViewController is %#", fullViewController);
self.fullViewController = fullViewController;
}
NSLog(#"self.navigationController is %#",self.navigationController);//this is null
[self.navigationController pushViewController:self.fullViewController animated:YES];
}
I'm not sure how to fix this. I've tried adding in the couple lines in the AppDelegate, but when it runs, the table in the root view doesn't show up and it no longer properly switches between portrait and landscape views.
I have the rest of the code readily available if that would help clarify. Just let me know!
Thanks.
From the code you post it is not possible to identify the problem, but two common reasons for self.navigationController to be nil are:
you did not push the object behind self on to the navigation controller in the first place; indeed it seems so, since the navigation controller is added as a subview of the split view controller; possibly you mean the opposite... not sure...
(sub-case of 1) you showed the object behind self using presentViewControllerModally.
When I say "the object behind self" I mean the instance of the class where pressFullViewButtonFunction is defined.
If you need more help, post the code where you push your controllers on to the navigation controller...
On a side note, if you do:
UINavigationController *nvcontrol =[[UINavigationController alloc] initWithRootViewController:fullViewController];
and nvcontrol is not an ivar, then you have a leak.
Hope this helps...

Resources