在initWithFrame之前调用viewDidLoad?

时间:2012-01-22 02:49:51

标签: iphone objective-c ios ipad

我有一个自定义初始化方法:

- (id) initWithFrame:(CGRect ) frame andImage:(UIImage *) image
{
    self = [super init];
    if (self){
        self.view.frame = frame;
        self.imageView_.image = image;
        self.imageScrollView_.frame = self.view.frame;
        imageOriginalFrame = frame;
        zoomedImageFrame = frame;
         NSLog(@"SCREEN DIM %f AND %f", zoomedImageFrame.size.height, zoomedImageFrame.origin.y);
    }
    return self;
}

以及我如何呈现它:

FullSizeImageViewController * fullSize = [[FullSizeImageViewController alloc] initWithFrame:imageOriginalFrame andImage:image];
                    if ([self.delegate respondsToSelector:@selector(fullStoryViewController:presentModalViewController:animated:)]) {
                        [self.delegate fullStoryViewController:self presentModalViewController:fullSize animated:YES];
                    }

然而,令人惊讶的是我的viewDidLoad在initWithFrame之前被调用。这怎么可能?

我猜这是因为我称之为超级初始化?如果不是我该怎么做?

1 个答案:

答案 0 :(得分:8)

在<{em> viewDidLoad方法之前,您的initWithFrame:andImage:方法未被调用。在 viewDidLoad方法期间,您的initWithFrame:andImage:方法被称为

您的initWithFrame:andImage:方法包含以下行:

self.view.frame = frame;

这是此的简写:

[[self view] setFrame:frame];

所以你的方法是调用-[UIViewController view]方法。 -[UIViewController view]方法基本上是这样的:

- (UIView *)view {
    if (!_view) {
        [self loadView];
        [self viewDidLoad];
    }
    return _view;
}

尝试在viewDidLoad方法中添加断点。当它被击中时,查看堆栈跟踪。您会在其中找到initWithFrame:andImage: