0

ARC なしで .XIB を使用しています。NSMultableArray の値を別のビューに渡しています。[self presentModel...] を配置すると機能しますが、AnotherView をボタンで呼び出すと、AnotherView の NSMultableArray の値が null になります。

AnotherView.h

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

AnotherView.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」と書き込まれました

CurrentView.h

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

CurrentView.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 配列を 2 回初期化し、arrayOfTheAnotherView を設定する前に viewDidLoad が起動する可能性があります。これを行う方法はたくさんありますが、1 つの質問があります。配列のコピー、または同じ配列オブジェクトへのポインタだけを作成するつもりですか?

このように、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 に答える