我分配了一个 viewcontroller 没有 init ,但为什么一切正常
I alloc a viewcontroller without init , but why everything works fine
我在没有初始化的情况下分配了一个 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
Avi 在(他的?)评论中说对了。真倒霉。这就像说 "I've been driving my car for 30,000 km without changing the oil and it's running fine. Why does everybody say you have to change your oil?"
Init 为 class 执行设置,它是祖先 classes。可以肯定的是,有些设置没有完成,将来会导致问题。
创建对象的正确方法是 alloc/init。如果你不这样做,"the result is undefined."
我在没有初始化的情况下分配了一个 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
Avi 在(他的?)评论中说对了。真倒霉。这就像说 "I've been driving my car for 30,000 km without changing the oil and it's running fine. Why does everybody say you have to change your oil?"
Init 为 class 执行设置,它是祖先 classes。可以肯定的是,有些设置没有完成,将来会导致问题。
创建对象的正确方法是 alloc/init。如果你不这样做,"the result is undefined."