No root view controller error on Xcode 5 - xcode

I get this error on Xcode 5:
Application windows are expected to have a root view controller at the end of application launch
My code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[Flurry startSession:#"HJ4X7X6PB3942VGHQWWZ"];
screenRect = [[UIScreen mainScreen] applicationFrame];
if (screenRect.size.height > 500)
{
isFive = YES;
NSLog(#"is iPhone 5");
}else{
isFive = NO;
}
[[SQLiteOperator sharedOperator] openDatabase];
[self setupLang];
[self handleTabBarLanguage];
[self handleAppUpdatePopUpLanguage];
_banner_view_controller = [[BannerViewController alloc] initWithNibName:#"BannerViewController" bundle:nil];
[_banner_view_controller send_request];
[_banner_view_controller didHidden];
[_navigation_controller.view addSubview:_banner_view_controller.view];
[CoreData sharedCoreData].banner_view_controller = _banner_view_controller;
// Create content and menu controllers
//
DEMONavigationController *navigationController = [[DEMONavigationController alloc] initWithRootViewController:[[DEMOHomeViewController alloc] init]];
DEMOMenuViewController *menuController = [[DEMOMenuViewController alloc] initWithStyle:UITableViewStylePlain];
// Create frosted view controller
//
REFrostedViewController *frostedViewController = [[REFrostedViewController alloc] initWithContentViewController:navigationController menuViewController:menuController];
frostedViewController.direction = REFrostedViewControllerDirectionLeft;
frostedViewController.liveBlurBackgroundStyle = REFrostedViewControllerLiveBackgroundStyleLight;
// Make it a root controller
//
self.window.rootViewController = frostedViewController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

If you are using storyboard click on the project name in project navigator in Xcode. Then look for deployment info. Select Main Interface as your storyboard name. In app delegate.m
- (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;
}
And now try it.

Related

UISplitViewControllerDisplayModePrimaryOverlay causes "Unbalanced calls to begin/end appearance transitions"

In iOS 8, setting the preferredDisplayMode on a UISplitViewController to PrimaryOverlay is generating the following warning:
"Unbalanced calls to begin/end appearance transitions for UINavigationController"
There is no problem if I set preferredDisplayMode to AllVisible or don't set it at all. Problem occurs for all iPads and iPhones in the simulator that I've tried. Problem occurs whether the app starts up in portrait or landscape.
Here's some very simple code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITableViewController *tableViewController = [[UITableViewController alloc] init];
UIViewController *viewController = [[UIViewController alloc] init];
UINavigationController *masterNavController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
UINavigationController *detailNavController = [[UINavigationController alloc] initWithRootViewController:viewController];
UISplitViewController *svc = [[UISplitViewController alloc] init];
[svc addChildViewController:masterNavController];
[svc addChildViewController:detailNavController];
//svc.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;
svc.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
self.window.rootViewController = svc;
[self.window makeKeyAndVisible];
return YES;
}
Wrap your display code in dispatch_async. Otherwise iOS seems to get confused with other animations running at the same time.
dispatch_async(dispatch_get_main_queue(), ^{
svc.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
});
or
dispatch_async(dispatch_get_main_queue()) {
svc.preferredDisplayMode = .PrimaryOverlay
}

How to use 2 storyboards for different screen sizes?

What and where do I put code for two storyboards one if they are running on a 3.5" screen and one for a 4" screen. I have two storyboards. One is named Main.storyboard and the other one is named Main2.storyboard.
The code goes into the AppDelegate.m file and looks similar to this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:#"AlternateStoryboard" bundle:nil];
UIViewController *vc = [mainstoryboard instantiateInitialViewController];
self.window.rootViewController = vc;
return YES;
}
You'll need to check the screen size first, of course, see the docs for UIScreen.
Something like this might help. In your AppDelegate's application:didFinishLaunchingWithOptions: method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone
&& [UIScreen mainScreen].bounds.size.height == 568.0) {
//is 4 inch screen
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController = [storyboard instantiateInitialViewController];
self.window.rootViewController = viewController;
}
else {
//is 3.5 inch screen
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main2" bundle:nil];
UIViewController *viewController = [storyboard instantiateInitialViewController];
self.window.rootViewController = viewController;
}
return YES;
}
The if/else statement checks the screen size and loads the appropriate storyboard.

How to Load View Controllers At the Beginning of the App?

I have a Tab View Controller as my root controller in an App which consists of 3 tabs let's call them View A, View B, View C.
I want to load these tabs as soon as my app starts, I think it can be done within didFinishLaunchingWithOptions function but I'm not exactly sure, does anyone know how to do it?
Thanks
For these kinds of purposes I used this approach:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
UITabBarController *tabBarController = [[UITabBarController alloc] init];
rootView = [[SGMenuViewController alloc] init];
UIViewController *tab1VC = [[UIViewController alloc] init];
UIViewController *tab2VC = [[UIViewController alloc] init];
UIViewController *tab3VC = [[UIViewController alloc] init];
UIViewController *tab4VC = [[UIViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:rootView];
UINavigationController *secondNavController = [[UINavigationController alloc] initWithRootViewController:tab3VC];
NSArray* controllers = [NSArray arrayWithObjects:navController, tab2VC, secondNavController, tab4VC, nil];
tabBarController.viewControllers = controllers;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
You basically create a UITabBarController and you put all your view there. You create a UINavigationController if you need to push/pop views in the selected tab. In the example above only the 1st and the 3rd tab have UINavigationController and so I can use self.navigationController pushViewController: on them.

Animation code stopped working

I have animation code in my AppDelegate file thats making the Default.png splashscreen to fade
out to the first controller of the app.
Since I implemented TabBar in my code, the animation stopped working.
Any idea why?
- (void) splashFade
{
splashView = [[UIImageView alloc]
initWithFrame:CGRectMake(0,0, 320, 480)];
splashView.image = [UIImage imageNamed:#"Default.png"];
[_window addSubview:splashView];
[_window bringSubviewToFront:splashView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView
setAnimationTransition:UIViewAnimationTransitionNone
forView:_window cache:YES];
[UIView setAnimationDelegate:self];
[UIView
setAnimationDidStopSelector:
#selector(startupAnimationDone:finished:context:)];
splashView.alpha = 0.0;
[UIView commitAnimations];
}
- (void)startupAnimationDone:(NSString *)animationID
finished:(NSNumber *)finished
context:(void *)context
{
[splashView removeFromSuperview];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
sleep(2);
[self splashFade];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
UIViewController *viewController2 = [[TableViewController alloc] initWithNibName:#"TableViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window addSubview:self.rootViewController.view];
[self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Try using
[self performSelector:#selector(splashFade) withObject:nil afterDelay:2];
better than sleep(2);

Init a UInavigationbar

I am having trouble in making a tableview with navigation bar or controller on top.
I have this piece of code,
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationController *addNavCon = [[UINavigationController alloc]initWithNibName:#"Welcome" bundle:nil];
self.navigationItem.rightBarButtonItem = self.addButtonItem;
self.navigationItem.leftBarButtonItem = self.editButtonItem;
[self createEditableCopyOfDatabaseIfNeeded];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];
NSString *documentDirectory = [self applicationDocumentsDirectory];
NSString *path = [documentDirectory stringByAppendingPathComponent:#"notebook.plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.Notes = tmpArray;
[tmpArray release];
}
However, the navigation bar never show up, while the table is doing fine. May i know what's the problem with the code?
Many thanks
You have created an instance of UINavigationController but never added it to anything. You need to add it to the current hierarchy or it will not appear.
If you wish to add the navController to the entire app, then you should do this in the App Delegate by
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UIViewController *rootController = [[MyRootViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:rootController];
[rootController release];
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
}

Resources