在呈现视图之前如何设置 UIImageView 的图像?

发布于 2024-10-15 16:47:14 字数 548 浏览 7 评论 0原文

我正在开发一个应用程序,为了节省我所做的视图,我希望能够动态地将视图传递给图像。例如,我创建一个视图:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

星星的軌跡 2024-10-22 16:47:14

您应该在相关 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.

请远离我 2024-10-22 16:47:14

也许你可以尝试这个:
添加两个新属性到您的 FlipsideViewController:

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

不要忘记在您的 FlipsideViewController.m 中综合它们。
然后,当您实例化 FlipsideViewController 时:

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

然后在 FlipsideViewController viewDidLoad 方法中,您可以将属性中的值分配给视图 IBOutlets:

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

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
}
榕城若虚 2024-10-22 16:47:14

确定您已在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文