2012-02-29 28 views
0

解决方案如下 - 并非真的是我认为的问题。无法在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愚弄。

对不起,感谢那些帮助。

+0

以前有没有可能,您没有正确地投射您的发件人或根本没有。我注意到你在成功的代码中将其转换为CityGuideAnnotation,但在示例中没有。 – CodaFi 2012-02-29 05:09:43

+0

请给出'annotate'的声明和实现。属性用于设置ivars,而对于'x.annotate = y'的等价设置方法应该是'[x setAnnotate:y]'。 – jrturton 2012-02-29 06:54:38

回答

0

错误是不是在使用属性的代码。我被调试器愚弄为指针。我将更加小心地记录未来的价值。

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

1

如果注释是目标vc上的私有属性,则发件人VC无法访问它(除了您设置的注释:方法)。 prepareForSegue不给予任何特定访问目标VC,在这种情况下是私人财产。如果你想使用点符号,你需要公开注释作为公共API的一部分。

我希望我正确地理解你的问题:-)

祝你好运,

达明

+0

不幸的是它不是私人的。编译器也没有抛出一个错误,只是没有设置flipside.annotation。 – Djehuty 2012-02-29 05:22:35

+1

它是一个UI插座?在prepareForSegue期间,插座未连接到目标vc。 – 2012-02-29 05:32:27

+0

感谢Damien的帮助,抱歉我没有及时弄清楚调试器的问题。 – Djehuty 2012-03-01 01:59:14

相关问题