0

さて、ビューに がありNSMutableArray、この配列の値を別のビューに渡したいと思います。[self presentModel view controller ...]しかし、作成した別のボタンを押したときに別のビューを表示したいだけなので、このメソッドを使用したくありません。問題は、ボタンを押して配列の値を表示すると、配列の値が失われ、空が[self presentModel view controller ...]返されることですが、メソッドを使用すると、値が正しく返されます。

現在のビュー:

@property and @synthesise AnotherViewController *superAnother


AnotherViewController *anView = [[AnotherViewController alloc]initWithNibName:nil bundle:nil];
superAnother.arrayOfTheAnotherView = [[NSMutableArray alloc]initWithArray:arrayOfTheCurrentView];

別のビュー:

@property and @synthesise NMutableArray *arrayOfTheAnotherView;

NSMultableArray array = [[NSMultableArray alloc]initWithArray:arrayOfTheAnotherView];

配列を使用してテーブル ビューをロードする:

cell.textLabel.text = [array objectAtIndex:indexPath.row];
4

3 に答える 3

0

ビューコントローラ間でNSMutableArrayを渡す別の方法は、次のようなものです。

  • Interface Builderで、最初のViewControllerのボタンにIBActionを追加します
  • 2つのviewControllerの間にストーリーボードセグエを追加します(1つのボタンから次のView Controllerにではなく、viewController間に直接)
  • このセグエに一意の識別子を与える
  • 最初のViewControllerのボタンからのIBActionで、行を追加します

    [自己performSegueWithIdentifier:@ "myIdentifier"];

  • 次に、最初のView Controllerで、デリゲートメソッドを実装できます。

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    
        if ([[segue identifier] isEqualToString:@"myIdentifier"]) {
    
            SecondViewController *con = [segue destinationViewController];
            con.array = self.array;
        }
    }
    
于 2012-08-24T17:08:59.543 に答える
0

arrayWithArray収集されたオブジェクトの他のインスタンスは作成しませんが、同じ参照を使用して別の配列インスタンスを作成します。ポップオーバーなどでデータソース オブジェクトを変更する場合は、arrayWithArray:copyItems:を使用して項目をコピーする必要があります。もちろん、含まれているすべてのオブジェクトは、NSCopyingコピーできるようにプロトコルに準拠する必要があります。

于 2012-08-24T16:17:30.453 に答える