我想使用故事板将数组传递到另一个类,并且我已经准备了以下代码,但是,日志显示可变数组为空,其中显然不是这种情况(在另一种方法中, log显示它不为null,只有在调用prepareForSegue时才变为null。这是为什么?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"confirmSegue"]) {
SBBookingConfirmation *bookingConfirmed=(SBBookingConfirmation*)segue.destinationViewController;
NSLog(@"dates chosen - %@", self.dateChosen);
bookingConfirmed.confirmedTimings=self.dateChosen;
}
}
输出:
dates chosen - (null)
答案 0 :(得分:3)
显然,您正在其他地方更改self.dateChosen
。这与prepareForSegue:
无关。
在调用segue之前(通过IB或方法)返回并检查您的操作。
答案 1 :(得分:0)
确保您的dateChosen属性类型为strong:
@property (nonatomic, strong) NSMutableArray *dateChosen;
//if you have an NSArray instead of NSMutableArray, use copy instead of strong
也在你的“方法”方法中(最糟糕的名字!),你应该使用setter来设置dateChosen:
-(void)method:(NSMutableArray *)array {
self.dateChosen=array; //not dateChosen = array;
NSLog(@"The following has been copied %@", self.dateChosen);
}
最后,您需要在某处初始化数组。如果你从未做过像
这样的事情self.dateChosen = [NSMutableArray array];
或
self.dateChosen = <NON-NIL array pointer>
难怪它不会是零。
旁注:更好地选择方法/变量名称。不要只调用方法“方法”。 如果使用数组,通常最好以复数形式命名:date * s *选择,而不是dateChosen。