3

プロトコルに問題があります。モーダル提示NavigationController(iOS 5 - ストーリーボード)を使用している場合、デリゲートが機能しません

私の「初期View Controller」はナビゲーションコントローラです。ルートページには、View Controllerが組み込まれている他のナビゲーションコントローラが表示されます。 segueをonclickする必要があります。これは完全に動作しますが、 "ViewController"のデリゲートメソッドは呼び出されません。

Iを加えた画像は、私はIOSの5

image

MyViewController.hにInterfaceBuilderで2 NavigationControllers間の接続を構築する方法の例である

@protocol MyProtocol <NSObject> 

@required 
- (void) myFunction:(NSString *)string; 

@end 

@interface MyViewController : UIViewController <MyProtocol> 

@property (nonatomic, assign) id<MyProtocol> delegate; 

@end 

MyViewController.m

#import "MyViewController.h" 
@implementation PropertyController 
@synthesize delegate = _delegate; 

- (void) myFunction:(NSString *)string { 
    [_delegate myFunction:string]; 
} 

- (IBAction) callDelegate:(id)sender { 
    ![enter image description here][1][self myFunction:@"test"]; 
} 

ViewController.h

上記
#import "MyViewController.h" 

@interface ViewController : UIViewController <MyProtocol> 

からNavigationControllerを示しているのViewControllerのコードViewController.m

#import "ViewController.h" 

@implementation ViewController 

- (void) myFunction:(NSString *)string { 
    NSLog(@"myFunction was called"); 
} 

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

- (IBAction) showModalNavigationController { 
    [self performSegueWithIdentifier:@"NameFromSegueInInterfaceBuilder" sender:self]; 
} 

は、私は私の問題の解決策を見つけるカントです。私は誰かが私に

を助けることを願って

はあなたに感謝:)私はここでの問題のカップル見

答えて

1

:ストーリーボードのスクリーンショットで

  1. を、あなたは、第2のナビゲーションコントローラを持っています。 PropertyControllerをナビゲーションコントローラに埋め込む必要はありません。代わりに、ルートビューコントローラに直接PropertyControllerをセグさせます。何らかの理由でナビゲーションコントローラが必要な場合は、prepareForSegueの実装を変更する必要があります。この場合、segue.destinationViewControllerUINavigationControllerを指しているためです。したがって、そのnavコントローラオブジェクトを取得し、そのnavコントローラオブジェクトのrootViewControllersetDelegateを送信する必要があります。しかし、再び、あなたがそのナビゲーションコントローラを保つことに決めた場合にのみ、
  2. MyViewControllerは、ViewControllerPropertyControllerクラスにどのように関係しますか? PropertyControllerクラス(またはスーパークラス)は、delegateプロパティに対して@propertysynthesizeのステートメントが必要です。
+0

こんにちは、ありがとう、私はその第2のナビゲーションコントローラが必要です。だから私はprepareForSegueの実装を変更する必要があります。どのように私はMyViewControllerからデリゲートを設定することができますか? – Haegar

+0

これはストーリーボードを扱う際の一般的な必要性なので、私はこのために 'UIStoryboardSegue'のカテゴリを実装します。私の[回答](http://stackoverflow.com/questions/8041237/how-to-set-the-delegate-with-a-storyboard/8041666#8041666)を別のストーリーボードの質問にしてください。 –

+0

ありがとうございました!今私はtopViewControllerからデリゲートをselfに設定し、今は動作します! :) [((MyViewController *)((UINavigationController *)segue.destinationViewController).topViewController))setDelegate:self]; – Haegar

0

xcodeが本当に好きではない作品が見つかりましたが、それでもまだ動作しています - あなたのデリゲートを設定すると、あなたのナビゲーションコントローラがデリゲートとしてあなたのビューを持っている場合、NSLogはあなたの宛先コントローラでself.delegateをnullにします。このDO防ぐには - あなたの目的地コントローラ(のviewDidLoad)に>

self.delegate = self.navigationController.delegate; 

を、それはあなたがナビゲーションコントローラ上で作成された委任を取得し、あなたよあなたの先のコントローラで使用できるようになります

ことがあります汚いですが、私が見つけた唯一の方法です。それが役に立てば幸い。

関連する問題