I've read Apple's "Your first iOS application" guide and everything there seems crystal clear to me. However, when I try to understand how View-based iOS application template provided in XCode works, I run into some interesting conundrums.
I understand that the application gets the main nib file name (usually, the MainWindow.xib) form the *-Info.plist f开发者_如何学JAVAile. What I do not understand is, how does the XCode know which nib file is associated with the controller that is created with this View-based application template by default. In the guide, you start with the Window-based application, and you "have to write" something like:
MyViewController *aViewController = [[MyViewController alloc]
initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
[self setMyViewController:aViewController];
which makes perfect sense. However, it turns out that in the View-based iOS application template there is no such thing, and that this nib specification was not actually needed in the first place, as long as you created your UIViewController subclass with option "With XIB for user interface" checked. My question is, how does XCode know which nib is associated with this controller, i.e. is it storing this connection in some of the files, or maybe by some sort of convention (same name for controller and nib file, perhaps)? Moreover, where does that 'Loaded from "MyViewBasedAppController"' subtitle come from in Interface builder's view of controller within MainWindow.xib? It's definitely not there when I add the controller by hand, so I'm curious to what magic does XCode do behind my back, when I think I'm just selecting a simple code template.
If you look in the target info (double click on the target to bring that up), 'Properties' tab you'll see the name of the main Nib file. The words 'Nib' and 'Xib' are interchangeable for these purposes; Xib is just a newer alternative for Nib.
It'll be 'MainWindow' fresh from the template. If you open MainWindow.xib you'll see that in there is an object called '[project name] App Delegate', and if you show the inspector and check under the 'i' tab you'll see the type of class that is named at the top. If you check the connections tab (the right facing arrow), you'll see that the File Owner (which is the UIApplication itself) has its 'delegate' property attached.
You'll also see that it has an outlet called 'viewController'. That's attached to another object in the xib called '[project name] View Controller'. Check the type on that and you'll see it's the type of view controller that Xcode has added to your project. Looking at its attributes (the first tab in the inspector, with the slider graphic), you'll also see that a separate nib file is specified as containing its main details.
For argument's sake, suppose I called my project 'NibTest' and made no changes.
At runtime, the device loads Info.plist. In there it sees that the delegate is of type NibTestAppDelegate. So it'll instantiate an instance of the class NibTestAppDelegate and set the UIApplication's delegate property to it.
It'll then see from MainWindow.nib that NibTestAppDelegate has a member named viewController of type NibTestViewController. So it'll create an instance of that and set the viewController property on the NibTestAppDelegate instance it just created to it.
In doing that it'll open the other xib and continue doing the same sort of steps.
Objective-C has a fully reflective runtime, so you can instantiate objects by their class name at runtime. This is one of the differences between Objective-C and C++, for example.
Xcode isn't generating any hidden code or relying on any hidden naming conventions. The whole thing is figured out at runtime by the OS.
EDIT: for example, in place of your example:
MyViewController *aViewController = [[MyViewController alloc]
initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
You could actually do:
MyViewController *aViewController = [[NSClassFromString(@"MyViewController") alloc]
initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
They'll operate identically as long as MyViewController exists in the program or in the wider runtime.
You could alternatively pass any other string object you like to NSClassFromString
. Even ask the user for it if you want (though it'd be a really bad idea for security reasons).
精彩评论