Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIApplicationDelegate in Interface Builder with Xcode 4.2

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:

  • Connect our UIApplicationDelegate implementation to the UIApplication's delegate outlet
  • Connect a UIWindow to the UIApplicationDelegate
  • Connect a 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]];
}
like image 320
iOS.Lover Avatar asked Feb 21 '23 22:02

iOS.Lover


1 Answers

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:

enter image description here

Open up main.m and replace the fourth parameter with nil:

enter image description here

Create a new xib, or hijack the existing one (which I'm going to do here), and make the File's Owner class UIApplication.

enter image description here

Next, add an Object from the toolbox and change the class to your UIApplicationDelegate subclass:

enter image description here

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.

enter image description here

Now add a UIWindow from the toolbox.

enter image description here

Now add a 'UIViewControllerfrom the toolbox. This is your customUIViewController. If you want aUINavigationControllerorUITableViewController` just add that instead.

enter image description here

If using a custom UIViewController class, specify the class here:

enter image description here

Connect the UIWindow's rootViewController outlet to your UIViewController:

enter image description here

Crack open your UIApplicationDelegate interface and make or modify the window property to make it an IBOutlet.

enter image description here

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.)

enter image description here

Switch back to your xib and hookup your UIApplicationDelegate's window outlet.

enter image description here

Now, in your target's deployment info, set your xib as the "Main Interface":

enter image description here

Done.... pshew!

like image 140
Steve Avatar answered Feb 25 '23 04:02

Steve