Objective-C application starts

When an Objective-C application starts, it starts by running the function named main(). It doesn't have to be in the file "main.m" but that's how the Xcode wizard sets things up.

Inside the wizard-produced main() function, there is this line:

int retVal = UIApplicationMain(argc, argv, nil, nil);

That is what starts the "UIKit" framework that makes up the entire application. Inside UIApplicationMain, an object of type UIApplication is created. And part of what UIApplication does when the application starts is call the applicationDidFinishLaunchingWithOptions method on the delegate member of the UIApplication class. This delegate is set up in the MainWindow.xib file to be an instance of your ProjectAppDelegate class, a subclass of NSObject that conforms to the UIApplicationDelegate protocol.

What prompts AppDelegate to initially run it's ...

Because in your MainWindow.xib file you have connected (well the project wizard did the connection actually) the File's Owner (which is the UIApplication object)'s "delegate" outlet to the UIApplicationDelegate object in the the .xib file, and the class of the UIApplicationDelegate is set to your app's UIApplicationDelegate subclass.

And there's nothing magic about "MainWindow.xib", it could be called "Foo.xib", what's important is that the property in your Info.plist file called "Main nib file base name" is "MainWindow". Trying renaming MainWindow.xib to Foo.xib and changing the "Main nib file base name" in your Info.plist to "Foo" and you'll see it still works.

EDIT: more about RootController

Again, there's nothing magic about the so-called "RootController". This is just the name of the UIViewController subclass created for you by the Xcode new project wizard.

The wizard places code in the project for two classes: ProjectAppDelegate and ProjectViewController. The ProjectAppDelegate class contains two outlet members:

IBOutlet UIWindow *window;
IBOutlet ProjectViewController *viewController;

in the MainWindow.xib file, instances of both UIWindow and ProjectViewController are placed, and hooked up to the above outlets in ProjectAppDelegate.

What gets your stuff up on the screen is this code in your ProjectAppDelegate class:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

Again, nothing really magic about this: the project wizard created code that adds your "root" ViewController's view to the window's view, and makes the window visible. Your "root" view controller was create in the .xib file, and hooked up to the ProjectAppDelegate outlet.

It is very instructive to try to create an application entirely by yourself without using any of the files from the wizard. You'll learn a lot about how .xib files work and how they relate to code objects.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值