For many beginners, they certainly want to try to create a window containing a view without the "single view application" template, but want to create an empty framework from scratch, then, add a view to it. Unfortunately, xcode4.6 does not include the "window-based application" option of Version 4.0 in the Start wizard, nor does it directly add the option of the uiviewcontroller subclass template, therefore, the methods described in many previous books are not applicable to beginners. In fact, it is easier to create a window from scratch in version 4.6.
1. Create an empty application. There is no simpler version than this. In the following options, the product name is windowbasedapp, and the class profix is WBA, as shown in:
2. Create a View Controller. To do this, first create a common class and select objective-C class:
3. Click "Next". On the page that appears, name the new class myviewcontroller. Select subclass as viewcontroller instead of nsobject. Select with XIB for user interface. Of course, if you want to create XIB again, you can do the same. The method will be described later.
4. Now, your project should be shown in. Note: To test whether your view can be opened, add a label to the view.
5. Run the simulator. In fact, you will find that the label you added does not appear.
6. Open wbaappdelegate. h and add the following content:
# Import "myappviewcontroller. H"
And
@ Property (nonatomic, retain)
Myappviewcontroller * viewcontroller;
7. Open wbaappdelegate. M and add the following three rows.
@ Synthesize viewcontroller;
Self. viewcontroller =
[[Myappviewcontroller alloc] initwithnibname: @ "myviewcontroller" Bundle: Nil];
[Self. windowaddsubview: viewcontroller. View];
To explain, add a viewcontroller instance. This sentence is very important because we just created a viewcontroller class, and this row created an instance associated with the XIB file. The third line added adds the Controller View to the window.
8. Run. Successful!
If Step 2 does not select to include XIB, you need to complete the following operations after completing the preceding steps (BTW: If you have already selected and do not want to redo it, You can delete the XIB file)
9. Add a user interface. Select view this time,
10. Start with a name, such as myviewcontroller,
11. You will add an XIB file myviewcontroller. XIB (of course there is also a view ). To test whether the operation is successful, add a lable in the view,
12. Of course, this is not a success, and the Controller has not been associated with the view.
Select the file's owner icon of the XIB, and change its base class from nsobject to myviewcontroller in its identity inspector. This class is created by yourself.
13. Press and hold the control key and drag the file's owner icon to the view icon. In the outlet panel that appears, select view as its output port ).
14. Get it done! Run the command again to display lable.