是在头文件中声明时分配和初始化对象,还是需要在我的实现中分配和初始化对象?
其中哪一个是正确的?
.h
@interface myViewController : UIViewController
@property (nonatomic, strong) UIImageView *bgImageView;
@end
.m
- (void)viewDidLoad
{
if (!self.bgImageView) {
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Background.png"];
self.bgImageView.image = [UIImage imageWithContentsOfFile:fullpath];
[self.view addSubview:self.bgImageView ];
[self.view sendSubviewToBack:self.bgImageView ];
}
或者
.h
@interface myViewController : UIViewController
@property (nonatomic, strong) UIImageView *bgImageView;
@end
.m
- (void)viewDidLoad
{
if (!self.bgImageView) {
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Background.png"];
self.bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:fullpath]];
[self.view addSubview:self.bgImageView ];
[self.view sendSubviewToBack:self.bgImageView ];
}