我使用以下代码更改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;
}你能告诉我上面的示例代码有什么区别吗?
发布于 2011-12-20 18:38:55
在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,因此您必须调用超级initWithNIB:或通过代码创建元素。
https://stackoverflow.com/questions/8574042
复制相似问题