首页 > 移动平台 > 详细

【iOS开发问题】添加UIImageView不显示的问题

时间:2015-05-13 19:46:24      阅读:444      评论:0      收藏:0      [点我收藏+]

今天用下面这个方法添加UIImageView但是运行却不显示

@interface ViewController ()
@property(nonatomic, weak)UIImageView *imageView;
@end

@implementation ViewController

 - (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *imageV = [[UIImageView alloc] init];
    imageV.frame = CGRectMake(100, 100, 100, 100);
    imageV.backgroundColor = [UIColor redColor];
    self.imageView = imageV;
   }

分析:

  • 在内存中创建了一个新的UIImageView对象,设置好了属性
  • 然后控制器中的imageView指针指向这个对象
  • 然后就是运行显示

发现问题:新创建的这个对象在内存中根本就不属于UIViewController,就算指向这个对象也不会显示的

正确做法

@interface ViewController ()
@property(nonatomic, weak)UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *imageV = [[UIImageView alloc] init];
    imageV.frame = CGRectMake(100, 100, 100, 100);
    imageV.backgroundColor = [UIColor redColor];
    [self.view addSubview:imageV];//把对象添加到控制器中
    self.imageView = imageV;
 }

【iOS开发问题】添加UIImageView不显示的问题

原文:http://blog.csdn.net/ttf1993/article/details/45696221

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!