3

私は次の設定を持っています:

それぞれに2つのラベルを表示するようにカスタマイズされたセルを持つTableViewparentTable含むViewControllerparentController

TableViewchildTable含むViewControllerchildController。このビューは、ユーザーがcontrollerParentのセルをクリックすると表示され、childTableの内容は選択したparentControllerセルによって異なります。私はこの方法を使用します:

[self.navigationController pushViewController:controleurEnfant animated:YES];

これで、childTableのセルをクリックすると、以前のビューに戻ります。

[self.navigationController popViewControllerAnimated:YES];

もちろん、選択したchildTableの行のインデックスを簡単に取得できます。しかし、私が知らない唯一のことは、私がそこに戻ったときに、このデータを保持してparentControllerで使用する方法ですか?

ご協力いただきありがとうございます...

4

1 に答える 1

3

この種の問題には、委任を使用できます

RayWenderlichs チュートリアルのコード:

あなたのchildViewControllerの.h:

@class ChildViewController;

@protocol ChildViewControllerDelegate <NSObject>
- (void)childViewControllerDidSelect:(id)yourData;
@end

@interface childViewController : UITableViewController

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

- (IBAction)cancel:(id)sender;
- (IBAction)done:(id)sender;

@end

.m の childViewController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 [self.delegate childViewControllerDidSelect:myObject];
 [self.navigationController popViewControllerAnimated:YES];
}

あなたのparentViewController.hでプロトコルを採用

@interface ParentViewController : UITableViewController <ChildViewControllerDelegate>

デリゲートメソッドを実装する

- (void)childViewControllerDidSelect:(id)yourData 
{
    self.someProperty = yourData
}

プッシュする前にデリゲートを設定することを忘れないでください:

...
ChildViewController *vc  = [ChildViewController alloc] init];
vc.delegate = self;
[self.navigationController pushViewController:vc animated:YES];

委任パターンに関するドキュメントは次のとおりです: Delegates and Data Sources

于 2012-05-26T16:02:47.993 に答える