在呈现视图之前如何设置 UIImageView 的图像?
我正在开发一个应用程序,为了节省我所做的视图,我希望能够动态地将视图传递给图像。例如,我创建一个视图:
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
然后我想在呈现视图之前设置视图的 imageview 显示的图像:
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...
但它不起作用!有人对此有什么想法吗?请帮忙!!
谢谢
注意:我确实在我试图呈现的视图上设置了 UIImageView 和 UILabel 属性...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该在相关 UIViewController 因为视图在初始化阶段不会存在,并且会在调用 viewDidAppear 时显示。
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.
也许你可以尝试这个:
添加两个新属性到您的 FlipsideViewController:
不要忘记在您的 FlipsideViewController.m 中综合它们。
然后,当您实例化 FlipsideViewController 时:
然后在 FlipsideViewController viewDidLoad 方法中,您可以将属性中的值分配给视图 IBOutlets:
Perhaps you could try this:
add two new properties to your FlipsideViewController:
Don't forget to synthesize them in your FlipsideViewController.m.
Then when you instantiate your FlipsideViewController:
and then in your FlipsideViewController viewDidLoad method you can assign the values in the properties to the view IBOutlets:
确定您已在 nib 文件中正确连接了 IBOutlet 吗?
假设您已将此代码放在视图控制器中的正确位置,那么这应该可以正常工作。因此,要么您不在运行此代码的位置,要么您正在配置的内容没有挂接到笔尖中的任何对象。它必须是这两件事之一。
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.