1

私は、次のことを実行するViewControllerを持ってUIButtonいます:

- (IBAction)buttonClicked:(id)sender 
{
    NSLog(@"Button clicked, lets move to next controller to do stuff");
    [self performSegueWithIdentifier:@"toNextController" sender:nil];
}

これは私の次へと移りますが、ViewControllerこれまでのところ驚くべきことは何もありません。

2番目のViewControllerでは、アプリケーションロジックの一部を実行してから、戻ります。

- (IBAction)backButtonPressed:(id)sender 
{
    NSLog(@"Back button clicked, lets just drop out of here...");
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)saveButtonPressed:(id)sender 
{
    NSLog(@"save button clicked, lets send some data back");

    [self performSegueWithIdentifier:@"backToMain" sender:nil];
}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{    
    if ([segue.identifier isEqualToString:@"backToMain"])
    {
        NSLog(@"Preparing segue for backToMain");

        // Obtain handles on the current and destination controllers
        MainController * startingViewController;
        SecondController * destinationController;

        startingViewController = (MainController * ) segue.sourceViewController;
        destinationController = (SecondController * ) segue.destinationViewController;

        // set data on the main controller
        startingViewController.myString = @"SomeDummyString";
    }
}

私がこれまでに試みたのは、メインコントローラーにリンクする2番目のセグエを作成し、セグエを実行する前に、そのハンドルをつかんでデータを設定することです。それが戻るのに最適な方法かどうかはわかりません。

質問:

を実行するときにデータを返すことは可能[self dismissModalViewControllerAnimated:YES];ですか、それとも戻りの旅のためにセグエを実装する必要がありますか?

4

2 に答える 2

1

あなたは次のようなことをすることができます

[destinationController setSomeData:@"Sending Something Back"];

これにより、ロードする前に、destinationControllerに@propertyが設定されます。

于 2012-06-01T16:32:50.777 に答える
1

これをチェックしてください:10841897

デリゲートとプロトコルを使用して情報をやり取りする方法について説明します。リンクに記載されてsavedWithData:いる一般的なものだけでなく、辞書や必要なデータを最初のViewControllerに送信するメソッドを作成するだけで、ニーズに合わせて少し変更できます。done

于 2012-06-01T16:47:47.263 に答える