0

私のアプリケーションでは、3つのビューコントローラが関係しています。1つ目は、2つ目のViewControllerを開くためのマップとボタンを含みます。2番目のViewControllerには検索可能なテーブルが含まれており、ユーザーが行を選択すると、3番目のViewControllerに関連データが読み込まれます。これはすべてうまくいきます!

ここでの意図は、ユーザーが3番目のViewControllerの[Showon Map]ボタンを押すと、データ(この場合は座標の2つのdouble値)を最初のView Controllerに戻し、最初のViewControllerができるようにすることです。これらの座標に焦点を合わせます。

私はAppleのドキュメント(BirdSightingチュートリアル)とSOに関する以前の質問/回答に従いましたが、1つの問題に気づきました。

thirdviewcontrollerのデリゲートを最初のviewcontrollerに設定する場所が本当に見つかりません。通常、最初のVCに次のコードを入力しますが、3番目のVCのインスタンスを作成していません。これは2番目のVCで発生します。

thirdVC.delegate = self;  //set self as the delegate

だから私は何をすべきですか?

ありがとう

4

3 に答える 3

1

デリゲートを secondViewController から thirdViewController に渡すか、たとえば通知センターをユーザーにすることができます。

NSString *const NotificationDataChanged = @"NotificationDataChanged";

NSDictionary *someData = @{};

[[NSNotificationCenter defaultCenter] postNotificationName:NotificationDataChanged object:someData];

たとえば、viewDidLoadに次の行を追加して、firstViewControllerでそれを観察する必要があります。

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(updateUserInfo:)
                                             NotificationDataChanged object:nil];

- (void)updateUserInfo:(NSNotification *)notification
{
    NSDictionary *someData = [notification userInfo];
}

dealloc でオブザーバーを削除することを忘れないでください:

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
于 2012-08-27T19:18:34.457 に答える
1

デリゲートは、必要なことを達成するための多くのメカニズムの 1 つです。@onnoweb による提案は完全に適していますが、これはデリゲート ポインターの受け渡しが面倒になる可能性があります。

KVO: データをモデル オブジェクトに入れ、VC3 でモデル オブジェクトを更新し、VC1 をそれらの値のオブザーバーにする KVO を検討することもできます。

NSNotificationCenter: 別の代替手段は NSNotificationCenter です

VC3 では、これを使用してブロードキャストを送信します (緯度/経度の座標を含むように辞書を設定します)。

[[NSNotificationCenter defaultCenter] postNotificationName:@"ShowOnMap" object:[NSDictionary dictionaryWithObjects:objects forKey:keys]];

VC1:

ブロードキャストを受信するために登録します。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onShowOnMap:) name:@"ShowOnMap" object:nil];

ブロードキャストを処理します。

-(void) onShowOnMap:(NSNotification *)notification
{
 NSDictionary *values = [notification object];
 .
 .
 . 
}

あなたのdeallocで登録を解除します

于 2012-08-27T19:19:15.257 に答える
0

最初の VC へのポインタを AppDelegate に保存して、呼び出すことができます

thirdVC.delegate =[(AppDelegate*)[NSApplication sharedApplication].delegate firstVC];
于 2012-08-27T19:18:39.633 に答える