2016-06-17 4 views
0

NSNotificationを使用してViewControllers間で値を渡していますが、値が渡されず、ブレークポイントを使用して何がうまくいかないかを確認すると、受信通知セレクタメソッドが呼び出されていないことがわかりました。 には、以下の私がNSNotificationセレクタメソッドが呼び出されない

AViewController.m

[[NSNotificationCenter defaultCenter] postNotificationName:@"speciality" object:nil userInfo:[specialityIdArray objectAtIndex:indexPath.row]]; 

BViewCOntroller.m

-(void)viewWillAppear:(BOOL)animated 
{ 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveSpecialityId:) name:@"speciality" object:nil]; 

} 

-(void)receiveSpecialityId:(NSNotification *)notificaton 
{ 

    NSString *selectedServiceString=[[notificaton userInfo] valueForKey:@"service"]; 

    _specialtiyId = selectedServiceString; 

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"service" object:nil ]; 
} 

私は BViewController

にAViewControllerから値を渡すしようとしていますが書かれているどのようなコードであります

私はすべてのディスカスを読んだシオンはこの同じ問題に以前に行われ、それらのどれもviewDidAppear

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

を追加

+1

'AVViewController.mの前に' BViewCOntroller.m'が初期化されていることを確認してください。 – iSashok

+0

新しいコントローラがviewWillAppearに達する前に通知が送信される可能性があります。 通知を送信するために使用したメソッドと2つのコントローラをロードするコードを投稿できますか? –

答えて

0

いけない」私の問題を解決していません。私は多くのObjective Cのを知りませんが、ここで取引が迅速である:

func receiveSpecialityID(notification: NSNotification){ 

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(<Theclassinwhichthismethodiswritten without the()).receiveSpecialityID(_:)), name: "Selected", object: nil) 
/// DO your thing here.. 
} 

その後の操作を行います。

これは動作するはず
deinit{ 
NSNotificationCenter.DefaultCenter().removeObserver 

} 

を。試してみてください!

関連する問題