2

質問

私はiOSアプリに取り組んでおり、3回連続していViewControllersます:

TableViewController--> DetailViewController-->ImageViewController

ボタンを使用して前方セグエを実行し(ドラッグを制御するだけStoryboard)、戻るにはカスタムの戻るボタンがあります

[self.navigationController popViewControllerAnimated:YES];

データを転送するには、使用します

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [segue.destinationViewController someFunction];
}

親にデータを送信するには でViewController使用できますprepareForSegueが、DetailViewControllerでは機能せず、ImageViewControllerを使用する必要がありますNotifications

?prepareForSegueでデータを送信するために使用しても問題ありません。popViewControllerAnimated

どちらの場合も通知を使用する必要がありますか?

いくつかのコード

私が持っているのDetailViewControllerは、ImageViewController へのセグエを実行するボタン (ドラッグを制御するだけStoryboard) と、次のような [戻る] ボタンです。

- (IBAction)backAction:(id)sender {
     [self.navigationController popViewControllerAnimated:YES];
}

そして関数:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"forwardSegue"]) {
        [segue.destinationViewController someFuntionToImageViewController];
    } else {
        [segue.destinationViewController someFuntionToParentViewController];
    }
}

アクションに segue.identifier を割り当てることができないことに気付きましたpopViewController

4

1 に答える 1

12

Apple recommends the following way to pass data between view controllers that are navigated by segues:

To pass data forward:

You declare a property on the destination vc and set its value in the prepareForSegue method. (So kind of the way you did it)

@interface SomeViewController

@property (strong, nonatomic) VeryImportantData *data;
//...
@end

@implementation SourceVC

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    VeryImportantData *theData = ...;
    segue.destinationViewController.data = theData;
}

To pass data back

You declare a delegate protocol. This could look like this:

@protocol SomeViewControllerDelegate
- (void)someViewController:(SomeViewController *) someVC didFinishWithData:(SomeData *) data;
@end

Your destination vc provides a delegate property:

@property (weak, nonatomic) id<SomeViewControllerDelegate> delegate;

The destination vc calls it's delegates method once it's ready to be closed:

@implementation SomeViewController

- (void) close
{
    [delegate someViewController:self didFinishWithData:self.data];
}

And your source controller implements the protocol and sets itself as delegate of the destination vc in the prepareForSegue method.

@implementation SourceVC

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    segue.destinationViewController.delegate = self;
}


//...
- (void)someViewController:(SomeViewController *) someVC didFinishWithData:(SomeData *) data
{
    self.receivedData = data;
    [self.navigationController popViewControllerAnimated:YES];
}
于 2012-10-28T17:06:13.967 に答える