Application windows are expected to have a root view controller - iphone

I have received a project from a client that I need to compile, but when I run it gives error Application windows are expected to have a root view controller at the end of application launch and EXC_BAD_Access (code=2, address=0x0)
as far as I know this is due to application:didFinishLaunchWithOptions: in the AppDelegate, but the problem with my project is that there is no AppDelegate file.
EDIT:
I tried to run the project on Xcode 4.5.1 now it is giving the error address doesn't contain a section that points to a section in a object file.

The problem is that there is no AppDelegate file. That is usually where the root view controller is set
- (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 = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
// set root view controller
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

I also experienced the "Application windows are expected to have a root view controller" error message.
If you are using a storyboard and you have verified:
The app delegate application:didFinishLaunchingWithOptions method is
returning TRUE and doing nothing else.
The view controller is set in IB to be the initial view controller.
The storyboard is appropriately set on the TARGETS Summary tab.
Verify that you have not created a "view" UIView #property and #synthezised it in the view controller implementation. I experienced this and through a lengthy process of elimination had my DOH moment when I realized I had created a "view" UIView property named exactly like the one IB had created and linked to the view controller.
Hope this helps.

Related

Where is UIWindow instantiated and the storyboard passed in?

I'm wondering if someone could explain (or point me in the right direction)
where the code for instantiating UIWindow disappears to when NOT using storyboards? In the empty-application project template the window is created in application didFinishLaunnching... in your AppDelegate.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
However if using storyboards, the above code is omitted, though obviously UIApplication knows which window to start off with.
Where the application looks for the info.plist file to know which storyboard(s) to start off with.
I'm certain this is all well documented somewhere I just haven't found it. Read this Where is the UIWindow instantiated in an iPhone app? but not much help. I've been at iOS for awhile, just never had to mess with the initial startup of an app until now. Thanks
I think you meant 'where the code disappears to when you are using storyboards.'
The application loads the storyboard according to the "Main storyboard file base name" (UIMainStoryboardFile) key in your Info.plist, and from that storyboard it loads the view controller with the "Is initial view controller" toggle set.
Edit: As asked in the comments, the following code (similar to the initial loading in xib-based apps) will allow you to load and display a storyboard by name upon application launch:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"StoryboardName" bundle:nil];
UIViewController *viewController = [storyboard instantiateInitialViewController];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
While using storyboards, the storyboard to be loaded comes from your app's Info.plist file.
There will be a section in your Info.plist file with the key value pair like this:
<key>UIMainStoryboardFile</key>
<string>MainStoryboard</string>
In this case, MainStoryboard is the name of the default storyboard that is loaded.

iPhone: unable to start simple application

I'm trying to make an iPhone app here, and I've gotten it down to a simple HelloWorld problem. For some reason, the following does not work in XCode 4.4. I'd really appreciate figuring out what's going on.
I follow these steps:
Start an 'empty project' type
Name it
Add in a new objective-c class with a .xib. Say this new view controller is StartViewController, so I now have StartViewController .xib, .h, and .m.
Check: file's owner for the .xib matches the .h file. It does in IB.
Change the background of the .xib to something other than black (I like stripes).
Add these lines to the main app delegate:
import "StartViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
StartViewController* theController = [[StartViewController alloc]init];
[self.window addSubview:theController.view];
[self.window makeKeyAndVisible];
return YES;
}
And the app immediately crashes on running with:
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "StartView" nib but the view outlet was not set.'
How can I make it work?
EDIT: PS, I have no idea why the code formatting appears to have failed. I was under the impression that it was just four spaces at the beginning of a line...
Do what am error tells you to do. Set an outlet for Startview in your VC.
I'm answering this because it's so ludicrous.
Turns out that order matters.
I deleted the old controlling files, and then added 'startingviewcontroller', edited the appdelegate function to be startingviewcontroller, then it all works. Not sure why that should matter, but hey.

SubView did not load in the first launching of my application

I'm new to iPhone development. I tried to build a simple application with a window and a navigation controller as a sub-view of this window. The problem is this: the sub view did not load when I launch the application. I just have a windows with black screen. To load the view controller, I have to quit the application and launch it a second time, then I have my sub view with the navigation controller. I added a button directly in the window to make sure that the black screen is not a problem, but I saw the button at startup.
This is the code I have in my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
[self.window addSubview:_navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Do you have any solution for this problem?
Thank you.
You need to make sure that you have your view hierarchy setup. The window's rootViewController will be the UINavigationController. The UINavigationController controls a hierarchy of viewControllers, so when you instantiate it, you need to assign a rootViewController. Often times this is a subclass of a UITableView.
Because you are alloc/initing the window, I'm assuming that you do not have a XIB/NIB with the UINavigationController and an associated rootViewController like a UITableViewController. Also, rather than adding the view of your navigation controller, you need to assign the rootViewController, to the window. Since iOS4 this is the preferred way of doing things. See here as well. Try this code:
YourViewController *yourViewController = /* code for alloc/initing your viewController */
_navigationController=[[UINavigationController alloc] initWithRootViewController:yourViewController ]
self.window.rootViewController=_navigationController; /* instead of using [self.window addSubview: _navigationController.view] */
[self.window makeKeyAndVisible];
If you are using a XIB/NIB, then you need to make sure the _navigationController is wired up to the XIB file and has a subclass of a viewController wired up as it's rootViewController.
Good Luck

iPhone: create new "View-based Application" = no view controller?

I have created a new iPhone "View-based Application" in Xcode. I then added a new "UIViewController subclass" and checked the "with XIB for user interface. Now the issue I have is that after hooking up all the variables and message handlers, I cannot push the new controller onto the stack using the following code:
[self.navigationController pushViewController:self.cabinetController
animated:YES];
All the variables and views are hooked up correctly, so all that I can think of is that its the way I am doing it, by pushing it onto the "navigationController". Is there something I am missing here? (I am very new to iPhone and Apple programming in general, so its probably a very simple oversight).
I realise that not enough information has been supplied ... here is a link to the project. Please note that it is an educational exercise has some creatively names classes.
http://files.me.com/nippysaurus/4yqz8t
In your appDelegate create a UINavigationController instance variable and then use your existing viewController as the rootViewController of the navigation controller.
e.g. in pure code using a UITableViewController (you can use xibs as well which your template app probably does).
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Create root view and navigation controller
UITableViewController *rootViewController = [[[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
// Not necessary if you're using xibs
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Add the nav controller's root view to the window
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
You need to change your view controller to a navigation controller, with its root view controller set as the current view controller.
If you examine your self.navigationController, you will realize it is nil. Messaging nil doesn't hurt, so no error message here.
Add another layer with a UINavigationController, and add your RandomShitViewController (nice name btw.) as its root view controller.
The navigation controller handles the push / pop part, your old controller manages its view.

TableView To Navigation Controller on the View-Based Application Project?

I built an application. On the one my views I used TableView. So now I want to change this Table view to a navigation controller.
1- How can I change UITable view to Navigation Controller.؟
The easiest way to achieve this is to create a new 'Navigation-based Application'. This will set up everything you need. By default this will set you up with simple RootViewController, if you want you can change this to be your TableViewController by editing the application delegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
rootViewController.managedObjectContext = self.managedObjectContext;
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
All you need to do is copy your TableViewController into the new project and change the RootViewController to be yours
If you have started with one of the template project in XCode find the .xib file for your view and launch InterfaceBuilder by double clicking the .xib file. You can manipulate the view directly by dragging and dropping components as well as adding components from a palette.
I would walk through one of Apples tutorials and get a feel for working with InterfaceBuilder - this tutorial shows adding a view controller.
Thank you everybody . finally i figured out :D , just put this code :
UINavigationController *myNav=[[UINavigationController alloc] initWithRootViewController:[[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil]];
[self presentModalViewController:myNav animated:YES];