segue:目标视图控制器古怪 [英] segue: destination view controller weirdness

查看:122
本文介绍了segue:目标视图控制器古怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用故事板和segues,我经常在执行segue之前将数据传递到目标视图控制器,如下所示:

In my app, I use a storyboard and segues, and I often pass data to the destination view controller before doing the segue, as follows:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([segue.destinationViewController respondsToSelector:@selector(setMyData:)]) {
        [segue.destinationViewController performSelector:@selector(setMyData:) 
                                          withObject:myData];
    }
} 

它可以在任何地方工作,除了在一个地方。选择器被调用,数据被设置,但是当segue完成并且目标控制器出现时,它没有我刚刚设置的数据。打印视图控制器的ID。在源和目的地视图控制器后,我发现 segue.destinationViewController 在上面的代码比一个视图控制器的不同实例显示出来。这里发生了什么?

It works everywhere except in one place. The selector gets called, the data gets set, but when the segue completes and the destination controller appears, it doesn't have the data I just set. After printing the view controller's id in both the source and destination view controllers, I found that the segue.destinationViewController in the code above is a DIFFERENT instance of the view controller than the one that gets displayed. What's going on here?

[更新1]
我查看了目标视图控制器的生命周期,它首先得到了在segue执行期间加载,但是我在它上面设置了属性!这意味着,当我在其上调用 performSelector 时,视图控制器对象未初始化!这就是我设置的数据不坚持的原因。我不明白为什么会这样,以及为什么同样的方法适用于我的应用程序的其他部分。

[UPDATE 1] I looked into the lifecycle of the destination view controller, and it first gets loaded during the segue execution, but AFTER I set the property on it! This means, that when I call performSelector on it, the view controller object is not initialized! This is why the data I set doesn't stick. t don't understand why is this the case, and why this same approach works in the other parts of my app.

[更新2]
按要求发布 setMyData 的代码。起初我根本没有这个方法,因为 locationToOpen 是一个公共属性。我只添加它以确保它被调用并打印调试信息。

[UPDATE 2] Posting the code of setMyData by request. At first I didn't have this method at all, because locationToOpen is a public property. I only added it to ensure it gets called and to print the debug info.

- (void)setMyData:(MyData *)myData
{
    DLog(@"self: %@", (id)self);
    _myData = myData;
}


推荐答案

我会这样做如下 -

I would do it as follows -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([segue.identifier isEqualToString:@"NextSegue"]) 
    {
        DestinationViewController *dest = [segue destinationViewController];

        dest.myData = self.myData; 
    }
}

在DestinationViewController.h中,你应该创建一个property -

And in the DestinationViewController.h, you should create a property -

#import <UIKit/UIKit.h>

@interface DestinationViewController : UIViewController

@property (nonatomic, strong) NSObject *myData;


@end

此外,请确保合成在DestinationViewController.m的myData的属性 -

And also, make sure to synthesize the myData property in DestinationViewController.m -

@interface DestinationViewController ()

@end

@implementation DestinationViewController

@synthesize myData = _myData;

// other methods here

@end

这篇关于segue:目标视图控制器古怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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