简体   繁体   English

无法在prepareForSegue:sender中设置属性:

[英]Can't set property in prepareForSegue:sender:

SOLUTION BELOW - Not really the problem I thought it was. 下面的解决方案-并不是我认为的真正问题。

I'm adding data to the view controller that's being segued into, using prepareForSegue:sender:, but my problem is that if the data is set using a property then that property is not changed. 我正在使用prepareForSegue:sender:将数据添加到被隔离的视图控制器中,但是我的问题是,如果使用属性设置数据,则该属性不会更改。 I can, however, use a private variable of the destination view controller, set using a function built for that purpose. 但是,我可以使用目标视图控制器的私有变量,该私有变量使用为此目的而构建的函数进行设置。

Here's the code that works: 这是有效的代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showLocationDetail"]) {
        CityGuideFlipsideViewController *flipside = [segue destinationViewController];
        CityGuideAnnotation *senderAnnotation = (CityGuideAnnotation *)sender;
        [flipside annotate:senderAnnotation]; // why?
    } 
}

It seems much more natural, though, to use flipside.annotate = senderAnnotation than [flipside annotate:senderAnnotation] . 但是,使用flipside.annotate = senderAnnotation[flipside annotate:senderAnnotation]更自然。

Surely I must be doing something obvious here, but I can't spot it. 当然,我必须在这里做一些明显的事情,但是我看不到它。

EDIT to give the fail case more clearly: 编辑以更清楚地给出失败案例:

// CityGuideFlipsideViewController.h
@interface CityGuideFlipsideViewController : UIViewController {
    CityGuideAnnotation *annotation;
}
@property (strong, nonatomic) CityGuideAnnotation *annotation;

// CityGuideFlipsideViewController.m
@synthesize annotation;
- (void)setAnnotation:(CityGuideAnnotation *)_annotation
{
    annotation = _annotation;
}

// CityGuideMainViewController.m (in prepareForSegue:sender)
CityGuideFlipsideViewController *flipside = [segue destinationViewController];
CityGuideAnnotation *senderAnnotation = (CityGuideAnnotation *)sender;
flipside.annotation = senderAnnotation;

On reaching the line assigning flipside.annotation as senderAnnotation the value of senderAnnotation is correct. 到达将flipside.annotation分配为senderAnnotation的行时,senderAnnotation的值正确。 flipside.annotation before assignment is nil. 分配前的flipside.annotation为零。 Following that line senderAnnotation is unchanged, flipside.annotation is unchanged. 该行senderAnnotation不变,flipside.annotation不变。

BUT, reaching CityGuideFlipsideViewController viewDidLoad I have NSLog(@"%@",annotation.title) which spits out the correct value, even though the debugger still shows me nil for annotation. 但是,到达CityGuideFlipsideViewController viewDidLoad我有NSLog(@"%@",annotation.title)会吐出正确的值,即使调试器仍然显示nil为注解。

So I'm really not sure if I previously had some minor error of if all along I've been fooled by annotation CityGuideAnnotation * 0x00000000 in the debugger. 所以我真的不确定我以前是否有一些小错误,即是否一直被调试器中的annotation CityGuideAnnotation * 0x00000000所欺骗。

Sorry about that, and thanks to those who helped. 对此感到抱歉,并感谢那些提供帮助的人。

If annotation is a private property on the destination vc, then the sender VC can't access it (apart from the annotate: method you set up). 如果注释是目标vc上的私有属性,则发送方VC将无法访问它(除了您设置的annotate:方法之外)。 prepareForSegue doesn't give any special access to the destinationVC, in this case a private property. prepareForSegue不授予对destinationVC的任何特殊访问权限,在这种情况下,私有属性是私有属性。 If you want to use dot notation you'll need to expose annotate as part of the public API. 如果要使用点表示法,则需要将注释公开为公共API的一部分。

I hope I understood your question properly :-) 希望我能正确理解您的问题:-)

Good luck, 祝好运,

Damien 达米安

Error was not in the code using a property. 使用属性的代码中没有错误。 I was fooled by the debugger showing nil as the pointer. 我被调试器愚弄了,显示nil作为指针。 I'll be more careful to log values in the future. 以后我会更小心地记录值。

See edit on question above for more details. 有关更多详细信息,请参见上面的问题编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM