Iphone UIImageView alloc]initWithImage使用IBOutlet不工作

Iphone UIImageView alloc]initWithImage使用IBOutlet不工作,iphone,uiimageview,Iphone,Uiimageview,我通常遵循在方法中创建对象的模式(如viewdiload),保留属性,然后释放创建的原始对象。比如说: NSArray *myArray = [NSArray alloc] initWithObjects:@"1", @"2", @"3", nil]; self.array = myArray; [myArray release]; 我想我可以用UIImageView做同样的事情。我在我的.xib中创建了一个UIImageView。我有一个出口,这是一个这样的财产 @property (non

我通常遵循在方法中创建对象的模式(如
viewdiload
),保留属性,然后释放创建的原始对象。比如说:

NSArray *myArray = [NSArray alloc] initWithObjects:@"1", @"2", @"3", nil];
self.array = myArray;
[myArray release];
我想我可以用
UIImageView
做同样的事情。我在我的
.xib
中创建了一个
UIImageView
。我有一个出口,这是一个这样的财产

@property (nonatomic, retain) IBOutlet UIImageView *imageView;
在my
viewDidLoad
中,我执行以下操作:

UIImageView *imgView = [[UIImageView alloc] initWithImage;[UIImage imageNamed:@"arrow.png"]];
self.imageView = imgView;
[imgView release];
如果我运行这个,我不会在屏幕上看到任何东西。但是,如果我只是在我的
viewDidLoad
中这样做:

[self.imageView setImage:[UIImage imageNamed:@"arrow.png"]];
我确实在屏幕上看到了我的箭头图像。我在这里缺少的
UIImageView
有什么不同之处吗

由于第一个响应而编辑:
UIImageView
是否与
UITableView
不同,因为我需要将其作为子视图添加到当前视图中?在
IB
中创建
UITableView
时,我不会在
viewDidLoad
中将其作为
子视图添加


谢谢。

您需要将
self.imageView
添加到视图层次结构中

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.imageView = ...

    // You are missing this part!
    [self.view addSubview:self.imageView];

    ...
}

另外,您不需要将
UIImageView
属性标记为
IBOutlet
,因为您没有使用
Interface Builder
构建视图。

您需要将
self.imageView
添加到视图层次结构中

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.imageView = ...

    // You are missing this part!
    [self.view addSubview:self.imageView];

    ...
}

另外,您不需要将
UIImageView
属性标记为
IBOutlet
,因为您没有使用
Interface Builder
构建视图。

您缺少了
IBOutlet
工作原理背后的要点。如果在
.xib
中指定了某个内容,则该内容已分配给您:)您只需使用它即可

因此
[self.imageView setImage:[UIImage imagename:@“arrow.png”]工作,因为您刚刚为其指定了一个图像

但是,什么时候,

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]];
self.imageView = imgView;
[imgView release];

实际上,您在
self.imageView=imgView处解除分配了由
IB
创建的
imageView
实例行并设置一个新的,并且也没有将其作为子视图添加到任何内容中。因为它是一个新实例,所以需要添加!!但是无论如何,如果您使用的是
IBOutlet

,那么这种方法是错误的,因为您忽略了
IBOutlet
工作原理背后的要点。如果在
.xib
中指定了某个内容,则该内容已分配给您:)您只需使用它即可

因此
[self.imageView setImage:[UIImage imagename:@“arrow.png”]工作,因为您刚刚为其指定了一个图像

但是,什么时候,

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]];
self.imageView = imgView;
[imgView release];

实际上,您在
self.imageView=imgView处解除分配了由
IB
创建的
imageView
实例行并设置一个新的,并且也没有将其作为子视图添加到任何内容中。因为它是一个新实例,所以需要添加!!但是无论如何,如果您使用的是
IBOutlet

,那么这种方法是错误的,它不是在.xib文件中创建时就已经添加了吗?就像当我有一个UITableView时,我不会在viewDidLoad中将它添加为子视图,对吗?当你在viewDidLoad中说self.imageView=imgView时,你在xib文件中创建的那个被覆盖了。如果您正在使用IB,请不要在ViewDidLoad中重新创建它。它不是在.xib文件中创建时已添加吗?就像当我有一个UITableView时,我不会在viewDidLoad中将它添加为子视图,对吗?当你在viewDidLoad中说self.imageView=imgView时,你在xib文件中创建的那个被覆盖了。如果您正在使用IB,请不要在viewDidLoad中重新创建它