How is an AppDelegate instanciated?
I have an iOS application for which I want to create a ViewController programmatically.
I started an empty XCode project and modified the main method so it looks like this
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"MyAppDelegate_iPad");
[pool release];
return retVal;
}
The app is a Universal Application, MyAppDelegate_iPad
is a subclass of MyAppDelegate
, which is a subclass of NSObject <UIApplicationDelegate>
.
My problem is that the applicationDidFinishLoading
method I've overridden in MyAppDelegate_iPad
is never called (break point on the first line never hits). The method looks like this
-(void) applicationDidFinishLaunching:(UIApplication *)application {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if(!window)
{
[self release];
return;
}
window.backgrou开发者_如何转开发ndColor = [UIColor whiteColor];
rootController = [[MyViewController alloc] init];
[window addSubview:rootController.view];
[window makeKeyAndVisible];
[window layoutSubviews];
}
I removed the line to link to a nib file from my plist file (I used to get the default "My Universal app on iPad" white screen) and now all that is displayed is a black screen. applicationDidFinishLoading
is still not being called.
Am I doing something wrong? How should I properly create my AppDelegate instance?
There’s a main nib file that bootstraps your application. This nib file is referenced in the Info.plist file under the NSMainNibFile
key and should contain an object that corresponds to your application delegate class (setting the Class attribute in Interface Builder). This application delegate object is referenced by the delegate outlet on the file’s owner placeholder.
So if I understand things correctly, the application loader loads the main nib file, setting itself as the nib owner. Its delegate
property gets set to a fresh instance of your application delegate class, and so the loader knows where to dispatch the various application lifecycle event callbacks.
There’s an awesome blog post about Cocoa application startup on Cocoa with Love.
If you are making universal you don't need two different app delegate classes. see this link (my answer), it may be help you to make universal app.
精彩评论