2012-01-22 107 views
2

我有一个自定义的init方法:viewDidLoad在initWithFrame之前被调用?

- (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之前调用。这怎么可能?

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

+0

它实际上通过ib – adit

回答

8

您的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:

+3

加载为了继续这一点,你不应该在你的init方法中做这种工作。所有这些应该在'viewDidLoad'中。你的'imageView'通常不会存在于你的init方法中,除了你通过引用'view'强制加载一个nib文件。 –

+0

那么我该如何给视图控制器提供图像和帧?我怎么会通过它? – adit

+1

您可以将其存储在实例变量中,稍后在'viewDidLoad'中使用。 –

相关问题