Instantiating UITableViewController in AppDelegate to work with Storyboard iOS - ios

Need help with a "Application windows are expected to have a root view controller at the end of application launch" warning in my console when running my app. It's a core data test I am working on. I am not getting the NSLog statements I am using for testing, only the previous message.
I created a new project from an Empty Application. My app delegate didFinish method code was generated to look like this:
- (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;
}
I added a storyboard and set it as the Main Interface. Then added a UITableView to the storyboard. Created a UITableViewController by adding a file and set it as the UITableView's class in the identity inspector.
It seems to get rid of the warning I should set the rootViewController. How do I set my UITableViewController as the rootViewController if I did not instantiate it in the appDelegate.m file? Alternately, If I do instantiate it in the appDelegate.m like so
UITableViewController *tableViewController = [[UITableViewController alloc]init];
self.window.rootViewController = tableViewController;
how do I associate tableViewController with corresponding .h and .m files?
Using Xcode 5.0.1, deployment target 7.0

When you add a storyboard to your empty application, and set the property "Main Storyboard file base name" in your Info.plist as the name of your storyboard, then the application instantiates your "window" object and assigns an instance of your storyboard's "initialViewController" as the "rootViewController" property of your window object. So you don't see the warning :
"Application windows are expected to have a root view controller at the end of application launch" when you do :
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}
This works fine.
However, in the code :
-(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;
}
If you were using storyboards, you have overridden default behaviour by creating a new window object, which no longer has the rootViewController provided by storyboard. In this case, you have to explicitly add a root view controller to your window object.
-(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];
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"YourStoryboard" bundle:[NSBundle mainBundle]];
YourTableViewController* vc = (YourTableViewController*)[storyboard instantiateInitialViewController];
_window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
Hope this helps !!

In StoryBoard set the desired controller as initialViewController.
In AppDelegate.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
youRootViewControllerObject = [storyboard instantiateInitialViewController];
This way you can access YouRootViewController class.

I did a lot of research and finally found out the right way to do this
in appDelegate.m instead of
- (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;
}
The code should simply be
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
See Sitepoint's helpful page for more detailed information
http://www.sitepoint.com/ios-application-development-storyboards/

Related

nothing is displayed when testing my app

originally when i tried to test my app i was getting this
[2685:70b] Application windows are expected to have a root view
controller at the end of application launch
and so i changed ccAppDelegate.m
From this
#implementation ccAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
To this
#import "ccAppDelegate.h"
#import "ccViewController.h"
#implementation ccAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ... Other code
// Override point for customization after application launch.
UIViewController *viewController = [[UIViewController alloc] init];
self.window.rootViewController = viewController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
now it is no longer crashing but nothing appears but a white screen
what can i do to fix this?
i have a copy of my work here if anyone wishes to have a look at all the code
https://drive.google.com/file/d/0B3_fSZXQB18lVzZuQVdpT3dnWjQ/edit?usp=sharing
as stated in the comments, you're creating and displaying an empty view controller as the root view controller. As result, you're seeing your brand new empty view controller.
If you want to use your view controller as the root one, you should change the line
UIViewController *viewController = [[UIViewController alloc] init];
to:
MyCustomViewController *viewController = [[MyCustomViewController alloc] init];
where obviously MyCustomViewController should be replaced with the name of your class ( I think it is ccViewController but I'm not sure)
Your method is good but to see anything onscreen you need to add anything rg. UIButton, UIImageView. I assume that you are doing everything in code so if this is hard for you why don't you try Storyboarding.
Anyway, just paste this code and you'll see a red background:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ... Other code
// Override point for customization after application launch.
UIViewController *viewController = [[UIViewController alloc] init];
self.window.rootViewController = viewController;
self.window.backgroundColor = [UIColor redColor];
[self.window makeKeyAndVisible];
return YES;
}
All I've changed in your code is [UIColor whiteColor] to [UIColor redColor].

Xcode without Storyboard and ARC

i have downloaded new xcode-5 and just started using it.
We can create application directly including storyboard and ARC , it is not asking for option like earlier versions.
So, my question is how can we use xcode5 without ARC and storyboard. we have to manually remove storyboard file ? or is there any other option.
Create a project with an Empty application and Add any viewcontroller (i added TestViewController here)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
TestViewController *test = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
STEPS FOR REMOVE ARC
1) In build setting set Automatic Reference Counting to NO.
///////////////////////////////////////////////////////////////////////////END///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
If you have Already Created Application with storyboard and ARC then
STEPS FOR REMOVE STORY BOARD
1) Remove Main.storyboard file from your project.
2) Add new files with xib for your controller , if it is not added in compiled sources in build phases then add there manually.
3) Remove Main storyboard file base name from plist.
4) Change appdelegate didFinishLaunchingWithOptions file and add :
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
[self.window makeKeyAndVisible];
just like :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
// Override point for customization after application launch.
TestViewController *test = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
Now,in above example you have to manage memory management manually like ,
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[test release];
STEPS FOR REMOVE ARC
1) In build setting set Automatic Reference Counting to NO.
Instead of delete the storyboard file, you can Create a new project with Empty Application template. So that you can avoid the storyboard file creation.
Use following steps to omit storyboard:
Create a new project with Empty Application template.
Add a new viewController (Example: LoginViewController)
Change the didFinishLaunchingWithOptions in AppDelegate.m file as specified below.
Change To:
#import "LoginViewController.h"
- (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];
LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:loginVC];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Remove ARC:
Go to Build Setting -> Objective-C Automatic Reference Counting -> NO
create new project
![Create new Project]
//remove Main storyboard file base name in Info
Add This Code In appdelegate
- (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];
LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:loginVC];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Then automatic remove your storyboard.
Please Try this...
successfully Executed. thanks
ShortCut: I Prefer
Create the project without Storyboard and ARC in xcode4 and then open that project in xcode5 .
Xcode 4 had the "Use Storyboards" checkbox when you created a new project. It is possible to grab the old Xcode 4 application templates (XML files) and convert them to Xcode 5. That way, you get the old templates back that let you choose whether you want storyboards or not.
I wrote a script that does all that work for you: https://github.com/jfahrenkrug/Xcode4templates
After running the script, you will have an "Xcode 4" section in the "New Project" screen:
And then - Alas! - you get your beloved choices back:
You will need a copy of the Xcode 4 .app bundle from http://developer.apple.com/ios to use this script.
I have a Tip:
The First: I create my project by XCode 4.6 (Because this version is nearest to XCode 5).
Of course that with XCode 4.6, you can chose use or not using ARC, Storyboard.
The Second: After that I will open my Project with XCode 5.
=> I think that Xcode 5 will understand that project is use nonARC, and of course, do not have Storyboard.
I hope your project will work! :D

How to load different view controllers based if URL is provided on start in iOS

I have an app, it authenticates with Instagram using safari, and when it returns I want it to load table view right away, without the login view.
Here is what I do now:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
DVGViewController *myVC = [[DVGViewController alloc] initWithNibName:#"DVGViewController" bundle:nil];
self.viewController = [[UINavigationController alloc] initWithRootViewController:myVC];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
// some code skipped
UITableViewController *myTVC = [[DVGTableViewController alloc] init];
self.viewController = [[UINavigationController alloc] initWithRootViewController:myTVC];
return YES;
}
At the moment I still get first method to work every time, although second method is triggered too, since I get calculations from url with it and I know that. Why it doesn't load tableView I don't know.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
method will call if you app is come fro m specific URl like from facebook auth or from safari then this method will call
I have found an error. In second method I have never set window and rootViewController. Adding this code to second method helped.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

Changing "initWithNibName" to "storyboardWithName"

I followed some tutorial to create an open doors animation during the app launch but it's calling an
xib file and I want to call storyboard and I don' have enough experience with this.
Here's my code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[OpenDoorsViewController alloc] initWithNibName:#"OpenDoorsViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
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;
}
If you start a new iOS project and select 'Use storyboards', the Storyboard will be automatically preloaded for you.
Storyboard is a place with all the controllers (scenes) of your app, and to reference one, you'll need to use
UIViewController *controller = [[UIStoryboard storyboardWithName:#"storyboard" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"an identifier"];

iOS Application launch black screen, UINavigationController, Nib, RootViewController

I've got the following app, whose RootViewController is named TopicsViewController.
When I run it, there aren't any errors or breaks but the screen is black. No table, populated or empty, just a black screen. Not sure which of the following is happening:
Is there something wrong with my application didFinishLaunchingWithOptions method in relation to a parser initlizing in it?
Is it something to do with my nib file for the TopicsViewController?
I can show more code from my TopicsViewController class if needed.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
///////////////////////////////////////////
***initializing code for parser which populates TopicsViewController (not shown to save space)*****
///////////////////////////////////////////
UIViewController *rootController =
[[TopicsViewController alloc]
initWithNibName:#"TopicsViewController" bundle:nil];
navController = [[UINavigationController alloc]
initWithRootViewController:rootController];
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
Instead of:
[self.window addSubview:navController.view];
Write:
self.window.rootViewController = self.navController;

Resources