【问题标题】:What type of properties of destination controller are accessibile from prepareSegue?prepareSegue 可以访问目标控制器的哪些类型的属性?
【发布时间】:2012-10-03 22:50:16
【问题描述】:

当我尝试在 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。我想知道为什么会这样。提前谢谢!

【问题讨论】:

    标签: ios storyboard uistoryboard segue uistoryboardsegue


    【解决方案1】:

    第二个代码不起作用,因为 cvc.venue 没有指向任何东西——您还没有在 MapController 类中实例化场地对象。您的自定义对象没有什么特别之处,与您拥有的没有什么不同:@property (nonatomic) NSArray *array; -- 在您实例化一个新的数组对象之前,数组将为 nil。

    【讨论】:

    • 只是为了确保我理解正确,所以基本上这适用于任何设置属性的尝试?例如,如果 Venue 类是 NSString 的子类,并且我尝试设置它,那会起作用,对吧?
    • 很难说——我得看一个例子来说明你的意思。我不知道您所说的“财产财产”是什么意思。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    相关资源
    最近更新 更多