如何在 prepareForSegue iOS 上设置 imageView 图像? [英] How to set imageView image on prepareForSegue iOS?

查看:25
本文介绍了如何在 prepareForSegue iOS 上设置 imageView 图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 prepareForSegue 推送到 ViewController.当我推送时,我想在推送视图控制器中的 ImageView 上设置一个图像.这是我尝试过的,

I tried to push to a ViewController using prepareForSegue. When I'm pushing, I want to set an image on ImageView in pushed view controller. Here what I tried,

视图控制器

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    UIButton *btn = sender;
    if (btn.tag == 50) {
        if (jpegData) {
            [self saveTempImage:jpegData];
        }
        if ([segue.identifier isEqualToString:@"HomeView"]) {
            HomeViewController *vc = [segue destinationViewController];
            vc.backImageView.image = capturedImage;
            vc.isBackImage = true;
        }
    }
}

我在 HomeViewController 中有一个 ImageView.我试图使用这个 vc.backImageView.image = captureImage; 设置它的图像.capturedImage 不为空.但是ImageView中没有设置图片.

I have an ImageView in HomeViewController. I tried to set it's image using this vc.backImageView.image = capturedImage;. capturedImage is not null. But the image is not set in ImageView.

我该如何解决这个问题?

How can I fix this?

提前致谢!

推荐答案

1) 您应该将 backImageView 属性(以及 HomeViewController 的所有其他出口)设为私有,因为对于此视图, HomeViewController 是专门负责的,其他类不应能够操纵这个视图.这是当前 iOS 开发中的既定惯例.您可以通过在 HomeViewController .m 文件中的 @implementation 关键字上方添加类扩展来实现这一点.

1) You should make the backImageView property (and all the other outlets of HomeViewController) private, because for this view, HomeViewController is exclusively responsible and no other class should be able to manipulate this view. This is current established convention in iOS development. You can do this by adding class extension above the @implementation keyword in HomeViewController .m file.

@interface HomeViewControler ()

@property (nonatomic, strong) IBOutlet UIImageView *backImageView;
//...other private properties ...

@end


@implementation MyViewControler

在那之后,您需要将视图插座属性从 .h 接口文件移动到 .m 接口扩展名,以进行私有声明.

Right after that, you need to move view outlet properties from the .h interface file to the .m interface extension to have the declared privately.

2) 您也应该在扩展中创建一个名为 captureImage 的私有属性.

2) You should create a private property called capturedImage in the extension too.

@interface HomeViewControler ()

@property (nonatomic, strong) IBOutlet UIImageView *backImageView;
@property (nonatomic, strong) UIImage *capturedImage;

@end

3) 在 h 中声明一个公共方法.名为 configureWithImage 的文件

3) Declare a public method in h. file called configureWithImage

-(void)configureWithImage:(UIImage *)paramImage;

-(void)configureWithImage:(UIImage *)paramImage;

并实施它.像这样的文件

and implement it i m. file like this

-(void)configureWithImage:(UIImage *)paramImage
{
    self.capturedImage = paramImage;
}

4) 接下来,您需要确保在 imageview 中使用传递的图像,因为 HomeViewController 的 viewDidLoad 很有意义.

4) Next you need to make sure the passed image is used in the imageview, for that HomeViewController's viewDidLoad makes a lot of sense.

-(void)viewDidLoad
{
    [super viewDidLoad];

    self.backImageView.image = self.capturedImage;
    //...other code...
}  

5) 最后一步,在 prepareForSegue 中,您使用图像配置视图控制器

5) Last step, in prepareForSegue you configure your view controller with the image

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIButton *btn = sender;
    if (btn.tag == 50) 
{
    //...other code...
    if ([segue.identifier isEqualToString:@"HomeView"]) 
    {
        HomeViewController *vc = [segue destinationViewController];
        [vc configureWithImage:capturedImage];

        //...other code...
    }
}

故事的精神"是,通过拥有一个适当的公共接口作为配置的唯一入口点(配置方法),您可以将两个实体解耦.这意味着源视图控制器仅传递图像,而无需了解目标 VC 中发生的任何事情.然后图像由负责的目标视图控制器处理.

The "morale of the story" is that by having a proper public interface that is the sole entry point for configuration (the config method) you decouple the two entities. It means the source view controller merely passes the image without having to know ANYTHING about what happens in the destination VC. The image is then processed by the responsible destination view controller.

如果您改变主意并在 HomeViewController 中进行一些布局/视图内容更改(可能的过滤和处理图像以获得视觉效果)稍后,源视图控制器将不会受到影响at all 因为小麦发生在 HomeViewController 中是没有人关心的,并且您将保持公共配置方法完好无损.这意味着更改不需要在 prepareForSegue 中维护代码,只需要在目标 VC 中维护.

Should you change your mind and do some layout/view content changes in the HomeViewController (possible filter and process the image for visual effects) later in time, the source view controller will not be affected at all because wheat happens in HomeViewController is nobody else's concern, and you will keep the public configuration method intact. That means the change will not require to maintain code in prepareForSegue, only in the destination VC.

这篇关于如何在 prepareForSegue iOS 上设置 imageView 图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆