2011-02-03 15 views
3

NavigationControllerを使用して、アプリケーションのrootViewからviewControllerを「プッシュ」しています。デリゲートとNavigationControllerの問題

デリゲートを使用して、現在ロードされているビューとrootViewControllerをcomunicateしたいとします。私はNSNotificationCenterを使用してこれを行うことができましたが、通信は常に1対1になるため、この特定の状況の代理人を試してみたいです。押されたビューで

、Iは、ヘッダファイルの次のデリゲートprotocoleを宣言:

#import <UIKit/UIKit.h> 

@protocol AnotherViewControllerDelegate; 

@interface AnotherViewController : UIViewController { 
    id <AnotherViewControllerDelegate> delegate; 
} 

- (IBAction) doAction; 

@property (nonatomic, assign) id delegate; 

@end 


@protocol AnotherViewControllerDelegate <NSObject> 
- (void) doDelegatedAction:(AnotherViewController *)controller; 
@end 

のdoAction IBActionを鑑みてUIButtonに接続されています。私の実装ファイルでは、私が追加しました:私のRootViewController.hで

#import "AnotherViewController.h"  
@implementation AnotherViewController 

@synthesize delegate; 

- (IBAction) doAction { 
    NSLog(@"doAction"); 
    [self.delegate doDelegatedAction:self]; 
} 

を私はインターフェイス宣言にAnotherViewControllerDelegateを追加しました:

@interface RootViewController : UIViewController <AnotherViewControllerDelegate> {... 

と、この私の実装ファイルに

- (void) doDelegatedAction:(AnotherViewController *)controller { 
    NSLog(@"rootviewcontroller->doDelegatedAction"); 
} 

を残念ながら、それは動作していません。 rootViewControllerのdoDelegatedActionは呼び出されていません。私はそれがために、私はAnotherViewControllerをプッシュする方法のだ疑い:

AnotherViewController *detailViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherViewController" bundle:nil]; 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    [detailViewController release]; 

は、私はそのデリゲートはちょうどそれがプッシュされていた瞬間にRootViewControllerになるだろうということAnotherViewControllerに、どのような方法で、言うべきでしょうか?私は何か他のものを逃していますか?

+0

どこ 'delegate'に値を代入していますか? 'AnotherViewController'のインスタンスに、そのRentViewControllerのインスタンスがそのデリゲートであることを伝える必要があります。 –

答えて

1

AnotherViewControllerdelegaterootViewControllerに設定すると、すべてが正しく接続されます。

あなたがAnotherViewControllerを初期化している場合は、あなたのrootViewController、それは次のようになります。

AnotherViewController *detailViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherViewController" bundle:nil]; 
detailViewController.delegate = self; 
[self.navigationController pushViewController:detailViewController animated:YES]; 
関連する問題