解决方案如下 - 并非真的是我认为的问题。无法在prepareForSegue中设置属性:发件人:
我使用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
愚弄。
对不起,感谢那些帮助。
以前有没有可能,您没有正确地投射您的发件人或根本没有。我注意到你在成功的代码中将其转换为CityGuideAnnotation,但在示例中没有。 – CodaFi 2012-02-29 05:09:43
请给出'annotate'的声明和实现。属性用于设置ivars,而对于'x.annotate = y'的等价设置方法应该是'[x setAnnotate:y]'。 – jrturton 2012-02-29 06:54:38