简体   繁体   English

如何在init方法中更改nib uiimageview图像?

[英]How to change the nib uiimageview image inside init method?

I used the following code to change the image of uiimageview and this works for me 我使用以下代码来更改uiimageview的图像,这对我有用

-(void)viewDidLoad{
     [super viewDidLoad];
     UIImage *tempImage = [UIImage imageNamed:@"instant_poetry_small.png" ];
     drawImage.image = tempImage;
}


-(id)initWithImage:(NSString *)img{
    [super viewDidLoad];
    return self;
}

But i want to change image of uiimageview inside init method. 但我想在init方法中更改uiimageview的图像。 For this i tried the following code but it didn't change the image of 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;
}

Below is my application delegate method 下面是我的应用程序委托方法

- (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;
}

Can you tell me what is the difference between above sample code? 你能告诉我上面的示例代码有什么区别吗?

In the method viewDidLoad the nib is done loading and all UI elements are available. 在方法viewDidLoad ,nib完成加载并且所有UI元素都可用。 Don't call viewDidLoad from init since there is now way to tell if the view (read nib) is loaded. 不要从init调用viewDidLoad ,因为现在可以判断是否加载了视图(读取nib)。

The initWithImage methode your wrote isn't correct and does not work in that way. 您编写的initWithImage方法不正确,不能以这种方式工作。 If you do want to create a custom init , it more like this" 如果你确实想创建一个自定义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;
}

But this does not load any NIB, so you have to either call super initWithNIB: or create the elements via code. 但这不会加载任何NIB,因此您必须调用super initWithNIB:或通过代码创建元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM