开发者

Why can't I set UIViewController's imageView in other UIViewController?

I have two UIViewControllers.In the firstViewController,there is a button ,the button action is

SecondViewController *controller = [[SecondViewController allo开发者_Python百科c]init];
UIImage *image1 = [UIImage imageNamed:@"a.jpg"];
controller.imageView.image = image1;
[self presentModalViewController:controller animated:YES];

when the button is clicked,the SecondViewController's view come up ,but the imageview's image didn't change to "a.jpg".I need help ,thanks.


you should do something like:

FirstViewController

SecondViewController *controller = [[SecondViewController alloc] 
                                                  initWithAnImageNamed:@"a.jpg"];
[self presentModalViewController:controller animated:YES];

SecondViewController

-(id)initWithAnImageNamed:(NSString*)anImage
{
    self.imageView.image = [UIImage imageNamed: anImage];
    return self;
}

Side Note: a.jpg should be included in the bundle.


Instead of send the UIImage to the secondViewController you can send the string "a.jpg", and in the viewDidLoad of the seconviewController you implement the code

imageView.image = [UIImage imageNamed:imageName];


A view controller doesn’t load its view until it needs to. When you access its imageView property, you’ll get nil back if you call it before the view has been loaded. You can force a view controller to load its view by explicitly calling -view:

[controller view];

It’s a kludge, but it works.


I think you are not alloc init'ing the UIImageView object for SecondViewController.

Add

controller.imageView = [[UIImageView alloc] init];

Also make sure you have a.jpg in your project folder.


I have solved this problem.I set image as a property instead of setting imageview as a property.I set the secondViewController's image in the firstViewController,like this:

SecondViewController *controller = [[SecondViewController alloc]init];
UIImage *image1 = [UIImage imageNamed:@"a.jpg"];
controller.image = image1;
[self presentModalViewController:controller animated:YES];

then set the imageView in the secondViewController's viewDidLoad method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜