ios segue:目标视图控制器怪异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10858939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
segue: destination view controller weirdness
提问by Macondo2Seattle
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:
在我的应用程序中,我使用了storyboard和segue,并且我经常在做segue之前将数据传递给目标视图控制器,如下所示:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController respondsToSelector:@selector(setMyData:)]) {
[segue.destinationViewController performSelector:@selector(setMyData:)
withObject:myData];
}
}
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?
除了在一个地方,它在任何地方都有效。选择器被调用,数据被设置,但是当 segue 完成并且目标控制器出现时,它没有我刚刚设置的数据。在源视图控制器和目标视图控制器中打印视图控制器的 id 后,我发现segue.destinationViewController
上面代码中的视图控制器实例与显示的视图控制器实例不同。这里发生了什么?
[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.
[更新 1]我查看了目标视图控制器的生命周期,它首先在 segue 执行期间加载,但在我设置属性之后!这意味着,当我调用performSelector
它时,视图控制器对象没有被初始化!这就是我设置的数据不粘的原因。不明白为什么会这样,为什么同样的方法适用于我的应用程序的其他部分。
[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.
[更新 2]发布setMyData
请求的代码。起初我根本没有这个方法,因为它locationToOpen
是一个公共属性。我添加它只是为了确保它被调用并打印调试信息。
- (void)setMyData:(MyData *)myData
{
DLog(@"self: %@", (id)self);
_myData = myData;
}
回答by Arunabh Das
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;
}
}
And in the DestinationViewController.h, you should create a property -
在 DestinationViewController.h 中,您应该创建一个属性 -
#import <UIKit/UIKit.h>
@interface DestinationViewController : UIViewController
@property (nonatomic, strong) NSObject *myData;
@end
And also, make sure to synthesize the myData property in DestinationViewController.m -
而且,确保在 DestinationViewController.m 中合成 myData 属性 -
@interface DestinationViewController ()
@end
@implementation DestinationViewController
@synthesize myData = _myData;
// other methods here
@end
回答by Ben
I had this same issue. It turned out for me that the target ViewController property I was setting in my prepareForSegue: code was declared as weak because I had copied and pasted the property from one that InterfaceBuilder auto-created, but mine was not a Storyboard object. So my property was being released and zeroed by ARC on exit from prepareForSegue:. Making it a non-weak property fixed it.
我有同样的问题。事实证明,我在 prepareForSegue: 代码中设置的目标 ViewController 属性被声明为弱属性,因为我已经从 InterfaceBuilder 自动创建的属性中复制并粘贴了该属性,但我的不是 Storyboard 对象。所以我的财产在从 prepareForSegue: 退出时被 ARC 释放和归零。使其成为非弱属性修复了它。
回答by m1sk
I had a similar problem where the ViewController I was doing the segue to changed at some odd point, after a looking around a bit it seems that the segue created a new ViewController.
我有一个类似的问题,我正在执行转场的 ViewController 在某个奇怪的点发生了变化,环顾四周后,似乎转场创建了一个新的 ViewController。
To solve the data passing problem I used notifications.
为了解决数据传递问题,我使用了通知。
回答by emersonku
I know this thread is old and solution should be found. But since the final solution is not posted here, I would like to list one of the possible root cause (which is the one in my case). I had the same issue, trying to set the variable in the destination view controller of the segue. The root cause is that I forgot to instantiated ([[Class alloc]init]) the object variable. I need it in my case because I am setting the properties of the object instead of pointing to other object. Hope this help.
我知道这个线程很旧,应该找到解决方案。但由于最终解决方案未在此处发布,我想列出一个可能的根本原因(在我的情况下就是这个原因)。我遇到了同样的问题,试图在 segue 的目标视图控制器中设置变量。根本原因是我忘记实例化 ([[Class alloc]init]) 对象变量。在我的情况下我需要它,因为我正在设置对象的属性而不是指向其他对象。希望这有帮助。