在ViewControllers之间传递MutableArray

时间:2012-11-12 11:50:20

标签: ios objective-c nsmutablearray

  BookingDocumentsViewController *bdVc = [self.storyboard instantiateViewControllerWithIdentifier:@"BookingDocs"];
    bdVc.orId = rl_id;
    bdVc.docsArray =  self.documentsArray;
    [self.navigationController pushViewController:bdVc animated:YES];

我有以上代码段。我正在尝试加载一个新的viewcontroller并将其Mutable Array(docsArray)对象分配给当前视图的mutableArray(documentsArray< = this is not nil)

每当我执行上面的代码时,我都会收到EXC_BAD_ACCESS错误。

但如果我评论第3行。它工作但我不能让我的数组到新视图。我甚至试过[[NSMutableArray alloc] initWithArray:self.documentsArray];这也不起作用。

但是,如果我使用bdVc.docsArray =[[[NSMutableArray alloc] init];它可以工作,但我再也无法将我的可变数组放到新视图中。

编辑: 但是第二行有NSString值。他们可以毫无问题地通过。

我在这里做错了什么?

我在控制台中没有收到任何错误,相反,我得到了这个错误。

enter image description here

3 个答案:

答案 0 :(得分:2)

也许考虑使用Segue。它为您实例化目标viewcontroller。然后在源视图控制器实现

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

获取对目标viewcontroller的引用并设置其数据。

BookingDocumentsViewController *bdVc = [segue destinationViewController];
bdVc.docsArray = self.documentsArray;

答案 1 :(得分:0)

在BookingDocumentsViewController.h

@property(nonatomic, retain)NSmutableArray *docsArray;

您可以在BookingDocumentsViewController.m中执行此操作:

@synthesize docsArray;

- (void)viewDidLoad
 {
    NSmutableArray *array = [NSmutableArray alloc]initWithArray:docsArray];   
   [super viewDidLoad];
 }

然后当你推动视图

BookingDocumentsViewController *bdVc = [self.storyboard instantiateViewControllerWithIdentifier:@"BookingDocs"];
bdVc.orId = rl_id;
bdVc.docsArray =  self.documentsArray;
[self.navigationController pushViewController:bdVc animated:YES];
[bdVc Release];

答案 2 :(得分:0)

我想我发现了这个问题。一个非常基本的错误。在bdVc的viewDidLoad中,我有以下行,

NSLog(@"Booking Documents viewDidLoad : %@",self.docsArray.count);

这导致错误。 %@而不是%d。我想知道为什么xcode没有显示错误的正确理由。

谢谢大家的帮助。 :)

相关问题