当我尝试在 prepareSegue 方法中从目标控制器设置属性时,某些类型的属性是可能的,而另一些则不是。例如,假设我的目标控制器包含以下属性:
@interface MapController:UIViewController
@property (weak, nonatomic) IBOutlet UIWebView *streetView;
@property (strong, nonatomic) NSString *url;
@property (nonatomic) NSString *latitude;
@property (nonatomic) NSString *longitude;
@property (nonatomic) Venue *venue;
@end
顺便说一句,“地点”看起来像这样:
@interface Venue:NSObject
@property (nonatomic) NSString *latitude;
@property (nonatomic) NSString *longitude;
@end
以下代码有效:
/****** CODE 1 *******/
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"MapViewSegue"]){
MapController *cvc =
(MapController *)[segue destinationViewController];
NSIndexPath *selectedIndex = self.tableView.indexPathForSelectedRow;
Venue *v = (Venue *)[self.entries objectAtIndex:[selectedIndex row]];
/****** Note the difference from CODE 2 below! ******/
cvc.latitude = v.latitude;
cvc.longitude = v.longitude;
// At this point,
// both cvc.latitude and cvc.longitude are properly set.
}
}
但这一个不起作用:
/****** CODE 2 *******/
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"MapViewSegue"]){
MapController *cvc =
(MapController *)[segue destinationViewController];
NSIndexPath *selectedIndex = self.tableView.indexPathForSelectedRow;
Venue *v = (Venue *)[self.entries objectAtIndex:[selectedIndex row]];
/****** Note the difference from CODE 1 above! ******/
cvc.venue.latitude = v.latitude;
cvc.venue.longitude = v.longitude;
// At this point,
// both cvc.venue.latitude and cvc.venue.longitude are nil
}
}
正如我在代码中所指出的,似乎可以在 prepareSegue 中设置 NSString 属性,但如果我尝试实例化自己的对象,它最终会返回 nil。我想知道为什么会这样。先感谢您!