解决方案 - 不是我认为的问题。
我正在使用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)
吐出正确的值,即使调试器仍然显示我没有注释。
所以我真的不确定我以前是否有一些小错误,如果我一直被调试器中的annotation CityGuideAnnotation * 0x00000000
所欺骗。
对此感到抱歉,感谢那些帮助过的人。
答案 0 :(得分:1)
如果注释是目标vc上的私有属性,则发件人VC无法访问它(除了您设置的注释:方法)。 prepareForSegue不提供对destinationVC的任何特殊访问权限,在本例中为private property。如果要使用点表示法,则需要将注释公开为公共API的一部分。
我希望我能正确理解你的问题: - )
祝你好运,达明
答案 1 :(得分:0)
使用属性的代码中没有错误。我被调试器所愚弄,显示nil作为指针。我将来会更加谨慎地记录值。
有关详细信息,请参阅上面的问题编辑。