Change which view opens first (Xcode) [duplicate] - ios

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to change the default View Controller that is loaded when app launches?
So if I made an app and a certain view opens first by default, and decide I want to change which view opens first, how would I do that?

That is controlled in the method in your AppDelegate.m file (or whatever the title of your app delegate file is) called didFinishLaunchingWithOptions. For example, in a tab bar app I've created it looks like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the tab bar controller's current view as a subview of the window
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
All you have to do is change the value of the self.window.rootViewController. For example, let's say you want a MapViewController to become the first page to open. You could do something like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the tab bar controller's current view as a subview of the window
MapViewController *mvc = [[MapViewController alloc]initWithNibName:#"MapViewController" bundle:nil]; //Allocate the View Controller
self.window.rootViewController = mvc; //Set the view controller
[self.window makeKeyAndVisible];
[mvc release]; //Release the memory
return YES;
}

Related

Displaying View Controller on top of other when app starts does not work in ios8

I use following code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
window.rootViewController = self.viewController;
[window makeKeyAndVisible];
if (self.requireLogin){
[self.viewController presentViewController:self.loginViewController animated:NO completion:nil];
}
}
So i push loginViewController on top of viewController. This works ok on iOS prior to 8, but on iOS 8 you can see for small amount of time the viewController.
Is there simple way to present view controller on iOS 8 without showing what is behind it ?
Edit:
Noticed in log that it has also "Unbalanced calls to begin/end appearance transitions" so think ios 8 runs some animation on self.viewController. Is there way to stop it animating ?
Try this Hide Initially, then unhide it.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
window.rootViewController = self.viewController;
// Hide initially
window.rootViewController.view.hidden = YES;
[window makeKeyAndVisible];
if (self.requireLogin){
[self.viewController presentViewController:yourloginViewController animated:NO completion:^{
// Unhide now
window.rootViewController.view.hidden = NO;
}];
}
}
Based on the response , It seems to be not showing the splash screen, So my better suggestion is
As you knew the YES/No to show the login view(based on self.requireLogin), you make login view controller as rootviewcontroller of your window.
if (self.requireLogin){
window.rootViewController = yourLoginViewController;
}
else{
window.rootViewController = normalViewController;
}
Still you need to use the same strategy of yours then add one UIImageView on the normal View Controller, then present login view controller.
The option is yours:)

iOS Simulator shows either blank black or white screen

I am running one project with no problems at all, but the second won't show up in iOS Simulator. What it does show depends on what's in my didFinishLaunchingWithOptions method in AppDelegate.m.
Gives black screen:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
Gives white screen:
- (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.window makeKeyAndVisible];
return YES;
}
Any ideas for a fix?
If you use .xib:
In first case you see black screen because you even not created window, in the second - you created white window, but without root controller.
You need to specify rootViewController of the window:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
// Your don't need specify window color if you add root controller, you will not see window because root controller will be above window.
//self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [YourViewController new];
[self.window makeKeyAndVisible];
return YES;
}
If you use storyboard leave the first example:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
And make sure that in proj file you specified the main storyboard:
Also make sure that you set one of the view controllers in the storyboard as an initial:
I'm using XCode V7.3.1 on El Capitan, and I got the simulator with the white screen on even the simplest of single view applications. I figured the controls were being displayed off screen but didn't know why.
I fixed it by scaling the simulator window down to less than 100%. On the simulator menubar, go to Window->Scale and pick 50 or 75%, and for me, the controls showed up properly laid out.
If you chosen "use storyboard", it is normal that you have
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
at this point, when you run it, you should see the ViewController's view of the default view controller (if you didn't mess with the storyboard file).
Try to click on your storyboard, select the controller clicking on the first icon on the black bar under it, and then select the identity inspector (3rd button on the right side panel), check that the custom class is the class of the existing ViewController that Xcode created for you, making sure that it is the initial controller

Possible to have a different iOS splash screen on first load?

Would it be possible to show a different splash screen (launch image) on the first load of an application?
For example the first load would have a text ("Please wait while we setup your app..."), while the following loads would have another splash screen (no text for example).
No this is not possible, since you can not change the launch image because the main bundle is read only.
You could however present a view controller just after your application is started and have the text in this view.
Just return as soon as possible from the application:didFinishLaunchingWithOptions: and only load the view controller with the text in this method.
Then start doing what ever your app need to do, and dismiss the view controller when done.
No, the default image is there at a time when your app isn't yet executing any of your code (well mostly). The default screen is very quick to load on most modern devices, so instead of a splash screen, just use a normal view/view controller for setup.
As both previous answer of #nevan king and #rckoenes, you can present a view controller and have the text or image or whatever in this custom splashscreen.
Add a method on didFinishLaunchingWithOptions to present this view :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Create View Controller
RootViewController *rootViewController = [[RootViewController alloc] init];
// Create Navigation Controller
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
// SplashScreen
[self displaySplashscreen];
return YES;
}
Create displaySplashscreen:
#pragma mark - SplashScreen Methods
- (void)displaySplashscreen
{
// Create View
self.splashscreenViewController = [[SplashscreenViewController alloc] init];
// Display Splashscreen
[_window addSubview:_splashscreenViewController.view];
// Dismiss Splashscreen
[self performSelector:#selector(dismissSplashscreen) withObject:nil afterDelay:3.0f];
}
Create your SplashscreenViewController and build what you need in this view controller.

AppDelegate window.rootViewController Property

I have a problem with my iOS app. It is based on storyboards. So to set the rootViewController property it should be enough to set the "Initial View Controller" property in Interface Builder and the MainInterface-Property in the project settings to the name of my storyboard. Still I always get the message "Application windows are expected to have a root view controller at the end of application launch".
I do several things in the applicationDidFinishLaunching section but even if everything except return YES; is commented out, I get the message.
How can I fix this warning? Or can I ignore it as everything works?
Thanks a lot.
Are you using an activity indicator in your app delegate or root view controller by chance? If so, it might be setting itself as the root. Move the display of the indicator to somewhere after your main views are set up.
Try this code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
InitialViewController initial = [self.storyboard instantiateViewControllerWithIdentifier:#"STORYBOARDID"];
window.rootViewController = initial;
[window makeKeyAndVisible];
return YES;}

I want to put tabbar on second view

Usally ,tabbar always displays buttom.
like this.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
but I want to diplay tabbar on second view (only).
1-2 is connected by navicontroller
2 is #interface TableViewController :UITableViewController
how??
better to add the tab bar controller programmatically in secondviewcontroller viewdidload

Resources