繁体   English   中英

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

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

下面的解决方案-并不是我认为的真正问题。

我正在使用prepareForSegue:sender:将数据添加到被隔离的视图控制器中,但是我的问题是,如果使用属性设置数据,则该属性不会更改。 但是,我可以使用目标视图控制器的私有变量,该私有变量使用为此目的而构建的函数进行设置。

这是有效的代码:

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

但是,使用flipside.annotate = senderAnnotation[flipside annotate:senderAnnotation]更自然。

当然,我必须在这里做一些明显的事情,但是我看不到它。

编辑以更清楚地给出失败案例:

// 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;

到达将flipside.annotation分配为senderAnnotation的行时,senderAnnotation的值正确。 分配前的flipside.annotation为零。 该行senderAnnotation不变,flipside.annotation不变。

但是,到达CityGuideFlipsideViewController viewDidLoad我有NSLog(@"%@",annotation.title)会吐出正确的值,即使调试器仍然显示nil为注解。

所以我真的不确定我以前是否有一些小错误,即是否一直被调试器中的annotation CityGuideAnnotation * 0x00000000所欺骗。

对此感到抱歉,并感谢那些提供帮助的人。

如果注释是目标vc上的私有属性,则发送方VC将无法访问它(除了您设置的annotate:方法之外)。 prepareForSegue不授予对destinationVC的任何特殊访问权限,在这种情况下,私有属性是私有属性。 如果要使用点表示法,则需要将注释公开为公共API的一部分。

希望我能正确理解您的问题:-)

祝好运,

达米安

使用属性的代码中没有错误。 我被调试器愚弄了,显示nil作为指针。 以后我会更小心地记录值。

有关更多详细信息,请参见上面的问题编辑。

暂无
暂无

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

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