![](/img/trans.png)
[英]How to change image of UIImageView that is inside a UIButton programmatically?
[英]How to change the nib uiimageview image inside init method?
我使用以下代碼來更改uiimageview的圖像,這對我有用
-(void)viewDidLoad{
[super viewDidLoad];
UIImage *tempImage = [UIImage imageNamed:@"instant_poetry_small.png" ];
drawImage.image = tempImage;
}
-(id)initWithImage:(NSString *)img{
[super viewDidLoad];
return self;
}
但我想在init方法中更改uiimageview的圖像。 為此,我嘗試了以下代碼,但它沒有改變uiimageview的圖像。
-(void)viewDidLoad{
[super viewDidLoad];
}
-(id)initWithImage:(NSString *)img{
[super viewDidLoad];
UIImage *tempImage = [UIImage imageNamed:@"instant_poetry_small.png" ];
drawImage.image = tempImage;
return self;
}
下面是我的應用程序委托方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[TTURLRequestQueue mainQueue] setMaxContentLength:0];
TTNavigator *navigator = [TTNavigator navigator];
navigator.window = _window;
TTURLMap *map = navigator.URLMap;
[map from:@"tt://appPhotos" toSharedViewController:[PhotoViewController class]];
[map from:@"tt://photo/detail?name=(initWithImage:)" toSharedViewController:[PhotoView class]];
[navigator openURLAction:[TTURLAction actionWithURLPath:[NSString stringWithFormat:@"tt://photo/detail?name=%@",@"level_me_up_small.png"]]];
[_window makeKeyAndVisible];
return YES;
}
你能告訴我上面的示例代碼有什么區別嗎?
在方法viewDidLoad
,nib完成加載並且所有UI元素都可用。 不要從init
調用viewDidLoad
,因為現在可以判斷是否加載了視圖(讀取nib)。
您編寫的initWithImage
方法不正確,不能以這種方式工作。 如果你確實想創建一個自定義init
,它更像是“
-(id)initWithImage:(NSString *)img{
self= [super init];
if (self) {
// Create a property for this call where you keep the image.
self.drawImage = [UIImage imageNamed:img];
}
return self;
}
但這不會加載任何NIB,因此您必須調用super initWithNIB:或通過代碼創建元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.