My application is a view-based application. I need to create a UINavigationController
as my rootViewController
.
In the former version of Xcode, there was a xib file named mainWindow
in which we could:
UIApplicationDelegate
implementation to the UIApplication
's delegate
outletUIWindow
to the UIApplicationDelegate
UIViewController
to the UIWindow
's rootViewController
property.But now (Xcode 4.2) does not create this xib file!
So how can I create a custom UINavigationController
and connect it to my UIApplicationDelegate
implementation in InterfaceBuilder?
Here is my code in my UIApplicationDelegate
implementation:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
SDWebImageRootViewController *root = [[SDWebImageRootViewController alloc] initWithNibName:nil bundle:nil];
_navigationController = [[[UINavigationController alloc] initWithRootViewController:root] autorelease];
self.window.rootViewController = _navigationController;
[[_navigationController navigationBar] setBarStyle:UIBarStyleBlack];
[[_navigationController navigationBar] setTranslucent:YES];
[_window addSubview:[_navigationController view]];
}
First of all, yes, you still can use IB for this even in the latest version of Xcode, so I'm not sure where you're getting that from.
If you want to know how to specify your application delegate without IB, that's fairly simple:
In your main
method, simply use your application delegate's class name as the 4th parameter to UIApplicationMain
:
int main(int argc, char *argv[])
{
@autoreleasepool
{
int retVal = UIApplicationMain(argc, argv, nil, @"APPLICATION_DELEGATE_CLASS_NAME");
return retVal;
}
}
In fact, Xcode 4.2 does this for you by default when you create a view-based application from the template (although it doesn't use a static string... which is probably better than my suggestion honestly because it would get refactored if you use the built-in refactoring, etc):
NSStringFromClass([AppDelegate class])
To answer your followup question: ok , then what should I do ? to connect the UINC outlet in interface ?
Don't bother.
Still don't believe me? Fine... here's a tutorial... it's FIFTEEN steps! ... and absolutely pointless.
First, create the application:
Open up main.m
and replace the fourth parameter with nil
:
Create a new xib, or hijack the existing one (which I'm going to do here), and make the File's Owner class UIApplication
.
Next, add an Object
from the toolbox and change the class to your UIApplicationDelegate
subclass:
Now, switch back over to the File's Owner and connect the delegate
outlet to the UIApplicationDelegate
you added. Remove the view
reference if you're doing what I did and hijacked the existing xib.
Now add a UIWindow
from the toolbox.
Now add a 'UIViewControllerfrom the toolbox. This is your custom
UIViewController. If you want a
UINavigationControlleror
UITableViewController` just add that instead.
If using a custom UIViewController
class, specify the class here:
Connect the UIWindow
's rootViewController
outlet to your UIViewController
:
Crack open your UIApplicationDelegate
interface and make or modify the window
property to make it an IBOutlet
.
Switch into the implementation and remove any "worthless" code that sets up your window and root view controller. (I'm being sarcastic here... this succinct code does everything we are doing here... just programmatically instead of through IB.)
Switch back to your xib and hookup your UIApplicationDelegate
's window
outlet.
Now, in your target's deployment info, set your xib as the "Main Interface":
Done.... pshew!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With