How to set up an iOS app by programmatically creating window and views?

584 Views Asked by At

I'd like to do away with XIB (Nib) files in my project, because that's just how I roll. However I'm finding that my AppDelegate class's method didFinishLaunchingWithOptions is never even called.

I tried setting the principal class in my app's main plist like so:

<key>NSPrincipalClass</key>
<string>AppDelegate</string>

This only leads to an error:

    +[AppDelegate registerForSystemEvents]: unrecognized selector sent to class 

So... how can I do without the principal class and just have my AppDelegate class run?

Thanks.

2

There are 2 best solutions below

0
On

The easiest thing to do is just start with an appropriate project template... choosing "Empty Application" does not create a nib, but does properly setup you main.m with the following crucial line:

return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

I think registerForSystemEvents is a private method on UIApplication, which makes sense since the principal class should be UIApplication or subclass thereof, not a responder conforming to UIApplicationDelegate.

The UIApplicationMain() call above will set your principal class to UIApplication and the delegate to your custom AppDelegate class.

But as I said - just make life simple by using the appropriate project template.

0
On

All C based programs need main.m, which is the starting point for all programs. You can choose a blank template, or you can just create your own. Though if you create your own you do need to make sure you've got an autorelease pool. You need to import your app delegate into main.m and I'm pretty sure you need UIKit as well. Then add the following code (replacing your app delegate class as needed:

int main(int argc, char *argv[])
    {
        int retVal = 0;
        @autoreleasepool {
            retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        }
        return retVal;
    }