我是说,initWithNibName:bundle用于手动加载nib文件,initWithCoder将被用作替代? [英] Am I right in saying initWithNibName:bundle is used to manually load nib files and that initWithCoder would be used as an alternative?

查看:164
本文介绍了我是说,initWithNibName:bundle用于手动加载nib文件,initWithCoder将被用作替代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能使用initWithNibName:bundle看到我现在使用最新的XCode(5)。经过一些研究,我发现了一个替代方案:initWithCoder。

I can't use initWithNibName:bundle seeing as I'm now using the latest XCode (5). After some research I found an alternative: initWithCoder.

示例:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if (self){
       // code here
    }

    return self;
}

我想知道的是这是initWithNibName的替代方法吗?

What I'm trying to understand is how this is an alternative to initWithNibName?

目前正在学习一本面向ios的大书呆子牧场书籍,它是为ios6和旧版本的xode编写的,并且用coreLocation框架进行实验。

Currently studying with a big nerd ranch book for ios which was written for ios6 and previous versions of xode and experimenting with the coreLocation framework.

在下面的代码中,我已经替换了initWithNibName。我也在一个早期的教程使用相同的初始化程序,并且它的工作,但我有麻烦,如果我不能完全理解一章,在教程书中做这个。苹果文档并不总是有意义的。通常,stackoverflow答案和重新读取的组合有助于吸收。

In the code below I've replaced the initWithNibName. I also done this in an earlier tutorial using that same initializer and it worked but I have trouble moving on in tutorial books if I don't fully understand a chapter. The apple docs don't always make sense instantly. Usually a combination of stackoverflow answers and re-reading helps things sink in.

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if (self){
        //create location manager object
        locationManager = [[CLLocationManager alloc] init];

        //there will be a warning from this line of code
        [locationManager setDelegate:self];

        //and we want it to be as accurate as possible
        //regardless of how much time/power it takes
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

        //tell our manager to start looking for it location immediately
        [locationManager startUpdatingLocation];
    }

    return self;
}

上面的代码是什么?它看起来像一个指定的初始化程序,但是参数的名称和返回类型阻碍了我。

What is the code above doing? It looks like a designated initializer but the name and the return type of the argument baffle me. Would appreciation some help here.

更多:

更新:

根据我在XCode 5中收集的内容,鼓励使用故事板,并且我没有看到不使用故事板的选项。

From what I've gathered in XCode 5 the use of storyboards is encouraged and I don't see an option to not use storyboards. The tutorials I'm following from this book are using XCode 4.3 where nibs were available.

推荐答案

NSCoding https://developer.apple.com/library /mac/documentation/cocoa/reference/foundation/Protocols/NSCoding_Protocol/Reference/Reference.html

为了理解这种方法的用法对于来自nib(或storyboard)的视图控制器,你必须理解NSCoding。

In order to understand what is going on with this method in regards to a view controller from a nib (or storyboard), you must understand NSCoding.

当使用NSCoding取消存档对象时,你会得到一个级联效果它拥有。 initWithCoder:被发送到一个对象,它被解冻,然后被发送到它拥有的对象等。

When objects are unarchived with NSCoding, you get a cascade effect for all objects it it owns. initWithCoder: is sent to one object, it is unfrozen, it is then sent to the objects it owns etc.

这是nib加载系统用来解冻所有

This is what the nib loading system uses to unfreeze all the objects you created in interface builder.

这是一个快速概要的nib加载系统(从文档)

Here is a quick rundown of what the nib loading system does (from the docs)


  1. 将nib文件和引用的资源加载到内存中

  2. 在nib中创建的对象图是未归档的(NSCoding)的对象。

  3. 所有插座和动作连接都已建立(您的电话号码已经被设置)您的电话号码

  4. awakeFromNib然后被发送到nib中的所有对象

  1. The nib file and referenced resources are loaded into memory
  2. The object graph created in the nib is unarchived (NSCoding) This actually depends on the type of object. UIViews are sent initWithFrame, UIViewControllers are sent initWithcoder since they conform to NSCoding and all other objects are just sent init.
  3. All outlets and action connections are established (Your IBOUtlets and IBActions) using setValue:forKey: and setTarget:action: respectively.
  4. awakeFromNib is then sent to all objects in the nib

在这里查看更多细节,在对象加载过程部分。
https://developer.apple.com/library/ ios / documentation / cocoa / conceptual / LoadingResources / CocoaNibs / CocoaNibs.html

Look here for more details under the object loading process section. https://developer.apple.com/library/ios/documentation/cocoa/conceptual/LoadingResources/CocoaNibs/CocoaNibs.html

问题是initWithCoder会在使用nib或storyboard时从viewController中调用因为这是系统如何解冻对象图,以及在界面构建器中对这些对象设置的属性。

The point is initWithCoder will be called from your viewController when using a nib or storyboard because that is how the system unfreezes your object graph, and the properties you set on those objects in interface builder.

还要记住,storyboard只是一个nib文件的集合其中一些元数据描述了它们之间的关系。

Also remember that a storyboard is just a collection of nib files with some metadata describing how they are related.

这篇关于我是说,initWithNibName:bundle用于手动加载nib文件,initWithCoder将被用作替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆