0

我正在使用 .XIB 并且没有 ARC。我将 NSMultableArray 的值传递给另一个视图,如果我放置 [self presentModel...],它可以工作,但是如果我用按钮调用 AnotherView,AnotherView 的 NSMultableArray 的值为 null!

另一个视图.h

@interface AnotherViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{
NSMutableArray *otherAnother;
NSMutableArray *arrayOfTheAnotherView;
}
@property (retain, nonatomic) IBOutlet UITableView *tableView;
@property (retain, nonatomic) NSMutableArray *arrayOfTheAnotherView;

另一个视图.m

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

otherAnother = [[NSMutableArray alloc]init];
otherAnother = [[NSMutableArray alloc]initWithArray:self.arrayOfTheAnotherView];
//    [otherAnother addObjectsFromArray:arrayOfTheAnotherView]; 
NSLog(@"%@", otherAnother);
NSLog(@"%@", arrayOfTheAnotherView);
NSLog(@"%@", self.arrayOfTheAnotherView);
}

3 NSLog 写了“null”

当前视图.h

@interface CurrentViewController : UIViewController {
NSMutableArray * arrayCurrentView;
AnotherViewController *superAnotherView;
}
@property (retain, nonatomic) AnotherViewController *superAnotherView;

当前视图.m

@synthesize superAnotherView;
NSString *x = [[NSString alloc]initWithFormat:@"%@",[label text]];

arrayCurrentView = [[NSMutableArray alloc]init];
[arrayCurrentView retain];
[arrayCurrentView addObject:x];

self.superAnotherView = [[AnotherViewController alloc]initWithNibName:nil bundle:nil];
self.superAnotherView.arrayOfTheAnotherView = [[NSMutableArray alloc]init];
[self.superAnotherView.arrayOfTheAnotherView retain];
[self.superAnotherView.arrayOfTheAnotherView addObjectsFromArray:arrayCurrentView];

我不知道如何保留 NSMultableArray 的值,谢谢帮助。

4

1 回答 1

0

代码有一些问题,比如你初始化了 otherAnother 数组两次,并且 viewDidLoad 可能在你设置 arrayOfTheAnotherView 之前被触发。有很多方法可以做到这一点,但有一个问题。您是要创建数组的副本还是只是指向同一数组对象的指针?

如果您像这样为 AnotherViewController 创建自定义初始化语句,可能会使事情变得更简单。

- (id) initWithSomeArray: (NSMutableArray *) _arrayOfTheAnotherView{
    self = [super init];
    if (self){
        arrayOfTheAnotherView = [[NSMutableArray alloc] initWithArray:_arrayOfTheAnotherView];
        //Or if you didn't' want a copy
        //arrayOfTheAnotherView = _arrayOfTheAnotheView;

    }
    return self;
}
于 2012-08-29T14:42:04.123 回答