xcode4.2手动添加MainWindow.xib

来源:互联网 发布:电脑怎么没网络了 编辑:程序博客网 时间:2024/04/26 12:22
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.

Start with Empty Application template

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 an xAppDelegate.h and .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. Choose iOS > User Interface > Empty as template. Add Empty Interface Builder documentNext, choose iPhone, next give it the name MainWindow (.xib will be added automatically). By the way, the name of the file is not very important, but it’s nice to choose MainWindow, because that’s familiar.

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.

The empty design surface of MainWindow.xib

  • Change the class of File’s Owner to UIApplication

Change class of File's Owner to UIApplication

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

Add Object to the document

  • 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.

Change class of the object to xAppDelegate

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

Add a window to the document

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  hook it up in Interface Builder. The xAppDelegate.h should read something like this:

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

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

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

Link the application delegate

  • Control-Drag from the window outlet of the xAppDelegate to the Window.

Link the window outlet of the app delegate

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

Add a label for testing

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.

Set the Main Interface to MainWindow

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. In xAppDelegate.m, there was actually code that creates a window as well. Just put the method

- (BOOL) application:didFinishLaunchingWithOptions:

in comment.

we're done

 

 

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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 梦幻西游手游苹果手机打不开怎么办 拼多多笔订单被判定虚假发货怎么办 苹果手机微信接收不到信息怎么办 内衣穿32太小穿34空杯怎么办 楼下的把下水管道堵死了怎么办?? 微信绑定的手机号码已停机怎么办 绑定微信的手机号停机了怎么办验证 绑定手机停机微信怎么验证码怎么办 五菱之光雨刷开关手柄断了怎么办 快手家长控制模式密码忘了怎么办 有陌生人要求你拉他入群怎么办 手机一天接到多个陌生电话怎么办 阳光惠生活登录密码忘了怎么办 一个身份证注册了两个支付宝怎么办 买手办拆了发现坏了怎么办 六个月宝宝喝贝因美过敏怎么办 超市盘点少的货扣员工工资怎么办 孩子东西在学校被偷了怎么办 想退出却不舍的感情不知怎么办? 我被老板摸全身失眠有阴影怎么办 不知道绑定支付宝的微博账号怎么办 银行卡丢了补办后支付宝账户怎么办 未激活的银行卡被锁定了怎么办 手机网银上的钱被盗走了怎么办 支付宝支付失败银行卡扣了钱怎么办 小程序的二次验证码丢失怎么办 苹果手机不小心购买游戏币怎么办 在淘宝买的洗衣粉是假的怎么办 京东快递电话填错了怎么办 新买的手机卡被注册过支付宝怎么办 手机卡掉了别人登陆支付宝怎么办 手机卡注销了支付宝欠的钱怎么办 手机丢了支付宝和微信里的钱怎么办 新办的手机号已被注册支付宝怎么办 新办的移动卡不能注册支付宝怎么办 新办的宝卡支付宝注册过怎么办 办支付宝后银行卡被盗刷怎么办 微信扫二维码送平衡群发了怎么办车 拼多多没收到货点了确认收货怎么办 拼多多收货地址填错了怎么办 京东订单申请退款已发货怎么办