本文介绍了为什么我的对象没有通过segue正确传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有2个控制器,BarViewController和FooViewController。

Say I have 2 controllers, BarViewController and FooViewController.

FooViewController有一个名为imageView的UIImageView的插座:

FooViewController has an outlet to a UIImageView called imageView:

@property (nonatomic, weak) UIImageView *imageView;

BarViewController有一个UIButton按钮的插座。
BarViewController有一个从这个按钮到FooViewController的segue,名为BarToFooSegue(在故事板中完成)。

BarViewController has an outlet to a UIButton button.BarViewController has a segue from this button to FooViewController, called BarToFooSegue (done in storyboard).

当我运行以下代码时,在FooViewController上调用NSLog。 imageView.image,结果为nil,我的图像不会显示。为什么会这样?

When I run the following code, and call NSLog on FooViewController.imageView.image, the result is nil, and my image won't display. Why is this the case?

// code in BarViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if([segue.identifier isEqualToString:@"BarToFooSegue"]){
        NSURL *photoUrl = @"http://www.randurl.com/someImage"; // assume valid url
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:photoUrl]];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        [segue.destinationViewController setImageView:imageView];
    }
}

我尝试将FooViewController.imageView设置为强弱,但问题仍然存在:

I've tried setting FooViewController.imageView to strong instead of weak, but the problem remains:

@property (nonatomic, strong) UIImageView *imageView;

运行我的调试器,我注意到FooViewController中的imageView在prepareForSegue中正确更新:但是后来得到了之后将几行更新为一些新分配的imageView,其中@property image设置为nil。我不确定控制流的哪个部分导致这种情况,因为它发生在用汇编语言编写的行中。

Running my debugger, I notice the imageView in FooViewController is updated correctly inside prepareForSegue: but then gets re-updated a few lines afterwards to some newly allocated imageView with @property image set to nil. I am not sure what part of the control flow is causing this because it happened in lines written in assembly language.

我通过向FooViewController添加UIImage属性来使我的代码正常工作:

I got my code working by adding a UIImage property to FooViewController:

@property (nonatomic, strong) UIImage *myImage;

并更改prepareForSegue:在BarViewController中传递图像而不是imageView:

and changing prepareForSegue: in BarViewController to pass the image instead of the imageView:

// code in BarViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if([segue.identifier isEqualToString:@"BarToFooSegue"]){
        NSURL *photoUrl = @"http://www.randurl.com/someImage"; // assume valid url
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:photoUrl]];

        [segue.destinationViewController setMyImage:image];
}

并修改viewWillAppear:在FooViewController中:

and modifying viewWillAppear: in FooViewController:

- (void)viewWillAppear:(BOOL)animated{
    [self.imageView setImage:self.myImage];
}


推荐答案

致电 [segue.destinationViewController视图]; 在设置图像之前,这将导致加载视图层次结构,然后设置您的出口。

Call [segue.destinationViewController view]; before you set the image, this will cause the view hierarchy to be loaded and your outlets will then be set.

这篇关于为什么我的对象没有通过segue正确传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 01:31