xcode4.2手动添加MainWindow.xib

MainWindow.xib

In the XCode 4.2 beta, MainWindow.xib is no longer included by default in some project templates. This means that you have to get your application to work by writing some code, using another method, or by manually reconstructing MainWindow.xib. This post shows the latter. Let’s get started.

xcode4.2手动添加MainWindow.xib

If you create a new project in XCode 4.2 beta, and choose the Empty Application template to start from, change nothing and try running it in your iPhone 5.0 simulator, you will see an empty – black – screen. The only thing you get from the template is anxAppDelegate.hand.m.

We will now reconstruct our own MainWindow.xib, to get started with development the way you’re used to. So the next thing we do is add a New File to the project.<wbr>Choose<em>iOS &gt; User Interface &gt; Empty</em>as template.<img title="Add Empty Interface Builder document" height="85" alt="Add Empty Interface Builder document" src="http://www.trappers.tk/site/wp-content/uploads/2011/06/Step-2.png" width="325" style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial">Next, choose iPhone, next give it the name<em>MainWindow</em>(.xib will be added automatically). By the way, the name of the file is not very important, but it’s nice to choose<em>MainWindow</em>, because that’s familiar.</wbr>

Select the new File we just created. What we have now is an empty design surface, in what used to be Interface Builder. Here we’re going to change some things.

xcode4.2手动添加MainWindow.xib

  • Change the class of File’s Owner to UIApplication

xcode4.2手动添加MainWindow.xib

  • Find Object in the Library and drag it onto the Objects pane on the left.

xcode4.2手动添加MainWindow.xib

  • Change the class of that Object to the xAppDelegate class that was created by the template, you might also want to clear out the “Object” label.

xcode4.2手动添加MainWindow.xib

  • Add a Window to the Objects pane on the left.

xcode4.2手动添加MainWindow.xib

Now, let’s bind it all together. To do this, we first need to change some of the code in the xAppDelegate.h. We have to add IBOutlet to the window property it has, so that we can <wbr>hook it up in Interface Builder. The xAppDelegate.h should read something like this:</wbr>

@interface DemoAppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic)IBOutletUIWindow *window; @end

Don’t forget tosavethe file, otherwise Interface Builder will not be able to pick up the Outlet. Next we continue editing the MainWindow.xib

  • Control-Drag from thedelegateoutlet of the File Owner to the xAppDelegate object.

xcode4.2手动添加MainWindow.xib

  • Control-Drag from thewindowoutlet of the xAppDelegate to the Window.

xcode4.2手动添加MainWindow.xib

  • Just for this demo, I’m adding a label to the window.

xcode4.2手动添加MainWindow.xib

We’re not done yet, but we’re almost there.

  • Navigate to the project, and in the Summary tab, select MainWindow as the Main Interface.

xcode4.2手动添加MainWindow.xib

You can now run the project in the Simulator, and the window should show up. However there’s one last thing you might want to clean up. InxAppDelegate.m, there was actually code that creates a window as well. Just put the method

- (BOOL) application:didFinishLaunchingWithOp<wbr>tions:</wbr>

in comment.

xcode4.2手动添加MainWindow.xib

<wbr></wbr>

<wbr></wbr>

I hope this helps to understand exactly how an iOS app starts. The next thing you should do is add a ViewController, and push it onto the MainWindow. I’m not going to cover that here. Please leave your feedback in the comments.


转自:http://blog.sina.com.cn/s/blog_4f1d20f30100twtd.html