2

我有 aUIViewController有两个属性: aUIImageView和 a UiButton。它们都IBOutlets正确连接在Interface Builder. 我的问题是,当我实例化这个视图并设置两个属性时,只UIImageView设置了。我UIButton是一个custom按钮,我正在尝试为UIImage它分配一个。这是我的代码:

MyViewController *viewController = [[MyViewController alloc]initWithNibName:@"MyViewController" bundle:nil];

[viewController view];

viewController.myImageView.image = [UIImage imageNamed:@"image1.png"];

viewController.myButton.imageView.image = [UIImage imageNamed:@"image2.png"];

知道我做错了什么吗?

4

3 回答 3

2

似乎您需要使用特定方法来设置 UIButton 的图像,而不是访问属性的设置器。那个方法是:

[viewController.myButton setImage:[UIImage imageNamed:@"image2.png"] forState:UIControlStateNormal];
//Or some other variant of UIControlState such as UIControlStateSelected, UIControlStateDisabled, UIControlStateHighlighted, etc.
于 2012-08-06T14:26:23.227 回答
1

代替

viewController.myButton.imageView.image = [UIImage imageNamed:@"image2.png"];

[viewController.myButton setBackgroundImage:[UIImage imageNamed:@"image2.png"] forState:UIControlStateNormal];

应该管用

于 2012-08-06T14:26:06.507 回答
1

这不是您为按钮设置图像的方式,您应该使用

[viewController.myButton setBackgroundImage:[UIImage imageNamed:@"image2.png"] forState:UIControlStateNormal];
于 2012-08-06T14:26:48.490 回答