2015-11-08 28 views
0

我分配一个viewcontroller没有初始化它,但一切工作正常。 viewcontroller中的子视图工作正常。我分配一个viewcontroller没有初始化,但为什么一切正常

这是代码。

self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; 
[self.window makeKeyAndVisible]; 
self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[ViewController alloc]]; 

中的代码ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self test]; 
} 


- (void)test 
{ 
    self.view.backgroundColor = [UIColor whiteColor]; 
    CGRect frame = CGRectMake(50, 100, 200, 200); 
    UIScrollView *scrollView= [[UIScrollView alloc] initWithFrame:frame]; 
    [self.view addSubview:scrollView]; 
    frame= CGRectMake(0, 0, 500, 500); 
    UIImageView *myImageView= [[UIImageView alloc] initWithFrame:frame]; 
    [scrollView addSubview:myImageView]; 
    scrollView.contentSize = CGSizeMake(500,500); 

    scrollView.backgroundColor = [UIColor blackColor]; 
    myImageView.backgroundColor = [UIColor yellowColor]; 

    scrollView.contentOffset = CGPointMake(0, 0); 

} 


@end 
+0

'ViewController'实际上做了什么吗? – Arc676

+0

我将ViewController.m的代码放在问题描述中。 – Zentopia

+0

那么,如果我没有记错'init'主要是初始化变量。你似乎没有任何全局的,我非常肯定'ViewController'扩展了'NSObject',所以你可能不需要调用'init'。 – Arc676

回答

0

阿维这样做是正确的(他?)的评论。这是愚蠢的运气。这就像是说:“我一直在开着车行驶3万公里而不换油,而且运行良好,为什么每个人都说你必须换油?”

初始化为类设置,它是祖先类。可以肯定的是,有些设置还没有完成,将来会导致问题。

创建对象的正确方法是使用alloc/init。如果你不这样做,“结果是不确定的。”

+0

非常感谢! – Zentopia

相关问题