开发者

How can I set a UIImageView's image before I present the view?

I have an app that I am working on, and in an effort to save on views that I make, I want to be able to dynamically pass the view an image. So for example, I make a view:

FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];

Then I want to set the image that the view's imageview shows before I present it:

UIImage *image = [UIImage imageNamed: @"IMG_5010_2.jpg"];
[controller.imageView setImage:image];
[controller.label setText:@"HI"];//I am trying to do this too and it isn't working...

But it just isn't working!! Does anyone have any thoughts on this? Please help!!

Thanks

NOTE: I do 开发者_开发知识库have UIImageView and UILabel attributes set on the view I am trying to present...


You should set the image within viewDidLoad method of the relevant UIViewController as the view won't exist during the init phase and will have been displayed by the time viewDidAppear is called.


Perhaps you could try this: add two new properties to your FlipsideViewController:

@property (retain) UIImage *image;
@property (copy) NSString *labelText;

Don't forget to synthesize them in your FlipsideViewController.m. Then when you instantiate your FlipsideViewController:

FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
UIImage *image = [UIImage imageNamed: @"IMG_5010_2.jpg"];
controller.image = image;
controller.labelText = @"Hi";

and then in your FlipsideViewController viewDidLoad method you can assign the values in the properties to the view IBOutlets:

- (void)viewDidLoad {
    //do other stuff
    [self.imageView setImage:self.image];
    [self.label setText:self.labelText];
    //any other stuff
}


Sure you've got your IBOutlets hooked up properly in the nib file?

Assuming you've put this code in the right place in your view controller, this should be working fine. So it's either you're not in a place that this code is getting run, or the things you're configuring aren't hooked to any objects in the nib. It has to be one of those two things.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜