ios应用程序的两种启动方式

时间 : 15-09-14 栏目 : iOS, 移动开发 作者 : noway 评论 : 0 点击 : 328 次


一:对于 window base 的创建步骤:

第1步:将创建的xib设置为启动frame。这个可以在 target>>summary>>main interface中设置。

这有点相当于将应用的启动入口设置为本xib。

第2步:这个启动xib的file owner一定是 UIApplication。但真正的全局AppDelegate是哪个呢?

那就需要你在xib中新建一个Object,然后把类名设置为您的真正的启动代理类:xxxx_AppDelegate

注意:这里需要将启动类绑定到 file owner的delegate。(和UI元素的绑定方法一样)。

我们再看看main函数:

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

这里没有指定启动的代理类。我猜想最后一个参数不设置的话,就会从启动xib中的UIApplication.delegate来指定。

第3步:继然是window base,那你当然需要在启动xib中创建一个window。

创建的window与代码如何对应呢?一般我们是在 xxxx_AppDelegate中创建一个UIWindow。

然后在xib中将window与代理中的实例连接上,然后代码中就直接用了,和使用一个控件一样。

这样,程序一启动,就创建了一个UIWindow。不需要你写UIWindow的创建代码。

第4步:作为一个UI程序,应该会有界面逻辑,而界面需要包含在UIViewController中,

所以,你可以在启动xib中创建UIViewController,绘制你的界面。或者直接关联别一个nib文件。

当然,你要使用它,仍然需要在xxxx_AppDelegate中声明UIViewController中的对象viewController,然后在xib中建立连接。

注意:这种方式下,UIViewController是自动创建的,不需要你写创建的代码。

最后一步:经过上面的动作。在启动代理的启动方法didFinishLaunchingWithOptions中添加以下代码就搞定了。

    [window addSubview:viewController.view];

    [window makeKeyAndVisible];

按照上面这种方式,我们的启动界面没有使用 alloc来创建任何ui对象,所有ui对象都是通过xib加载时自动生成的。

需要注意的是,这种方式,需要将xib设置为 main interface。如果您的程序是iphone和ipad适应的,那可以设置两个不同的启动xib.

如果在xcode4中要创建 window base project,差不多就上面的套路。靠xib来生成window和viewController.

注:我也见到过没有设置main interface的程序,并且在main中也没有设置AppDelegate类。我猜想,默认为使用MainWindow.xib做为

启动Interface。好象书中的hello_world就是这样的。

二:再看看single view project的创建方式。

第1步:当然,我们不能设置 main interface。我们需要看看main函数。在其中设定启动代理类。

UIApplicationMain(argc, argv,nilNSStringFromClass([SingViewTest_AppDelegateclass]));

在这里,第4个参数我们指定了启动的AppDelegate,而不是象上面那样,通过xib来指定启动代理类。

第2步:在启动方法中,自行创建window

    self.window = [[[UIWindowallocinitWithFrame:[[UIScreenmainScreenbounds]] autorelease];

  上面的代码创建一个全屏的窗体,看看,是我们自已alloc的。

 

第3步:通过程序来创建 SingelView的viewControoler.

    self.viewController = [[[SingViewTest_ViewControllerallocinitWithNibName:@"SingViewTest_ViewController"bundle:nil]autorelease];

  如果您的viewController想通过xib来加载(通常的方式),那就采取上面的方式。

如果您不想使用 UI 设计器,你也可以

    MyViewController *vc = [[ MyViewControlleralloc] init];

当然,这种情况你需要在viewdidLoad中动态生成您的ui元素。

最后一步:将viewcontroller的ui显示出来。

    self.window.rootViewController =self.viewController;

    [self.windowmakeKeyAndVisible];

 

通过上面两个工程的创建,可以看出,ios提供两个启动入口的方式。

方式一:指定xib做为启动interface,这种方式下,需要自已指定启动AppDelegate。

可以通过创建 object,然后改类名,再绑定的方式。

这种方式下,可以以此为起点,将真正的启动window,启动ViewController在此处创建和连接。

对于简单的应用,我们还可以在这个启动的xib里,将启动的ui都画好。

方式二:在main中指定启动AppDelegate。

这个需要修改 main函数中 UIApplicationMain方法的最后一个参数。

这种方式下,启动的windows,viewController需要自已通过alloc来生成,后面的做法和方式一就没啥区别了。

本文标签

除非注明,文章均为( noway )原创,转载请保留链接: http://blog-old.z3a105.com/?p=542

ios应用程序的两种启动方式:等您坐沙发呢!

发表评论





       ==QQ:122320466==

 微信    QQ群


0

Aujourd’hui, une partie avec le développement du e-commerce, achats en ligne est devenu une partie de la vie pour beaucoup de gens. La mariage a commencé achats en ligne. Si vous choisissez achats les mariages en ligne, il peut être beaucoup moins cher que le salon de la Robe de mariée pas chermariée local, réduisant le budget de mariage. vous pouvez avoir beaucoup de choix si acheter de mariage en ligne. vous pouvez ramasser une robe de mariée bon marché sur Internet.
Piercing fascinerande figur, och nu tittar vi på 2016 senast brudklänning, kan du vara den vackraste bruden det!2016 senaste Bra brudklänning, söt temperament Bra design, romantiska spetsar blomma kjol, som du lägger till en elegant och charmig temperament.Kvinnan tillbaka mjuka linjer, människor brudklänningofta få en känsla av oändlig frestelse. Fall 2016 mässan, lämnar uppgifter om ditt bröllop charmig.
Yesterday afternoon, the Chinese team was training in the Guangzhou Gymnasium, when the reporter asked Zhao Yunlei the feeling of wearing the new cheap jersey , cheap jerseys online shopshe readily took a shirt from the bag crumpled ball to reporters, and she said with a smile: ” This shirt is light. ”Zhao Yunlei said: “Our material is very light like with the clothes of the tennis King Nadal, Federer, after the sweat, sweat does not drip down to the ground, when we do move, it is easy pace slipping if the sweat drip on the floor.”Tennis players Zhang Yawen, told reporters: “You might think the clothes attached to the body, fearing we swing will be affected, in fact, we do not feel anything, because the clothes are very light, very soft, put on quite comfortable. And it’s particularly good clothes to dry, washing and will dry in 15 minutes. ”
China’s sports enthusiasts NFL sweatshirt with mad love and the pursuit of, and therefore, NFL jerseys have a good market in China and development. China is a populous country, is the consumer, the economic momentum is so good, the sales prospects sportswear is immeasurable. With hot sales sweatshirt, but also to promote the importance of sports fans, on health, on the other hand is a matter of concern for the World Cup, fans wearing NFL jerseys and also can express themselves more fully love and obsession Therefore, NFL jerseys Wholesale jerseys online shopwholesale has good prospects and development in China.
ANTA-ANTA Sports Products Limited, referred to as ANTA Sports, Anta, is China’s leading sporting goods companies, mainly engaged in the design, development, manufacture and marketing of ANTA brand sporting goods, including sports footwear, apparel and accessories. Anta sweatshirt design advantages, warm stretch knit fabric, using Slim version of model, more personal fit, bid farewell to bloated, so wearing more stylish.GUIRENNIAO-This logo is a spiritual totem, smooth graphics implication unstoppable force; flexible deliver an elegant arc Wholesale jerseys china shop movement, strength and speed of the United States, a symbol of passion and rationality publicity “Heart” and “meaning”, “concept” unity; pass the fearless and enterprising mind, showing beyond the realm of self, to unstoppable force to create the future.XTEP-Xtep (China) Co., Ltd. is a comprehensive development wholesale jerseys china shop, production and marketing of Xtep brand (XTEP) sports shoes, clothing, bags, caps, balls, socks mainly large sporting goods industry enterprises.
There are a lot of fans in identifying the authenticity of the above cheap jerseys have great distress, so here to i will show you some methods to definitely affordable inexpensive cheap jerseys : Firstly, we should look at if it is working fine. China has been called the world’s factory, a lot cheap jerseys factories in China have foundries, but our cheap jerseys are all from here! Secondly, should to see whether it is the , we all know that it is difficult to get out of print once a genuine cheap cheap jerseys free shipping jersey was print. and we have all kind of stocka on the whole website, in other words, we have all you want ! Finally, look at the price, our price is not necessarily the lowest in the whole website but it must be most fair on the whole website, we certainly you will not regret later when you buy it. Of course, except that cheap jerseys, we also have the other products, such as socks, leggings and some other related products, everyone can enjoy the best services of here!

KUBET