简体   繁体   中英

How to change UiImageView in xib file programmatically

Im trying to change UIImageView in xib file but it seem like it not working.I have xib file with UIImageView . I link my UIImageView to my xib file.In my main ViewController i call a function that will change the UIImageView inside the xib file.Below is my function to change the image in xib file

UIView *view = [[[NSBundle bundleForClass:[self class]]loadNibNamed:@"View" owner:self options:nil] firstObject];
[self addSubview:view];
view.frame = self.bounds;
_imageTest.image = [UIImage imageNamed:@"0.png"];

Did i do something wrong. I search in stack overflow but it seem like i cannot find what i looking for.

I am not sure if you are trying to change the image of UIImageView or the control itself with some other UI<control .

To change the UIImage , you just need an outlet in your viewController class and then set the image to it. Something as :

[self.yourImageView setImage:[UIImage imageNamed:@"0.png"]]; //and make sure the 0.png file exist in the application bundle.

If you are interested in changing the control itself, then its better not to create the UIImage as well. Create UIImage and the other control on runtime and add the controls to the View.

Try load your xib view like this:

UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"yourView" owner:self options:nil] objectAtIndex:0];

and put break point where you set image, make sure your _imageTest is not nil.

It's ok guys. I can make it work already. It seem like my UIImageView problem. After i delete and add back everything work already. Thank guys for your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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