3

在我之前关于UIViewControllers指定初始化程序的帖子之后,initWithCoder我还有一个关于aDecoder传递给协议方法的参数的问题。

这是有问题的代码:

@implementation WhereamiViewController

- (id)initWithCoder:(NSCoder *)aDecoder //we're overiding the superclasses (UIViewController) inititalizer
{
    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 its location immediately
        [locationManager startUpdatingLocation];
    }

    return self;
}

我对 aDecoder 很好奇,因此将其重命名以查看我的代码是否仍然有效并且确实有效。我想知道的是作为参数传递给 initWithCoder 的究竟是什么?我的代码中似乎没有任何内容。参数是否只是方法的一部分,即使没有传递给它也必须显示?其他时候我创建了指定的初始化程序,我是这样完成的:

self = [super init]

init 是 NSObjects 指定的初始化器吧?

这是我不明白的代码的唯一部分。我看到我正在调用我的超类初始化程序,然后用额外的自定义代码实现它/使其成为自 (whereamiviewcontroller) 值。

我确实设置了一个标记并查看日志,看看是否有什么能引起我的注意,但没有运气。

提前致谢
问候

4

1 回答 1

2

-initWithCoder:当您尝试从 nib 或情节提要初始化视图控制器实例时,您可以看到该方法的实际作用。在这种情况下,Cocoa Touch 将使用初始化程序的UINibDecoder实例从 xml 中解码控制器元素。-initWithCoder:

于 2013-10-17T23:37:00.960 回答