2013-04-18 19 views
29

私にはUIView->UICollectionView->UICollectionViewCellがあります。私はプログラム的に戻ることを試みていますが、これらの作品はありません。コードが呼び出されました。 私はStoryBoardを使用しています。ナビゲート前のビューコントローラーに戻る

- (void) goBack:(NSNotification *) notification { 
     // [self.navigationController popViewControllerAnimated:YES]; 
    // [self dismissViewControllerAnimated:YES completion:nil]; 
     [self.navigationController popToRootViewControllerAnimated:YES]; 
} 
+0

あなたのボタンのアクションコードまたは通知コードを表示しますか? – Balu

+0

self.navigationControlleがあなたのクラスでnilではないようになっていますか?これをチェックして。 –

+0

[[NSNotificationCenter defaultCenter] postNotificationName:@ "goBack"オブジェクト:なし]; – user1688346

答えて

83

あなたは使用する必要があります。

[self.navigationController popToRootViewControllerAnimated:YES]; 

これは、ルートビューコントローラにあなたを取り戻すでしょう。我々は我々がすることができますラインの下に使用することにより、親ビューコントローラ

[self.navigationController popViewControllerAnimated:YES]; 

に行くことができますラインの下に使用することにより

[self.navigationController popViewControllerAnimated:YES]; 
+0

このコードは私のために働いていません –

+0

あなたのrootViewControllerとしてUINavigationControllerを設定しましたか?そうでない場合は、動作しません。 –

0
- (void) goBack:(NSNotification *) notification 
{ 
    if(!self.YOrView.isHidden) 
     self.YOrView.hidden = YES; 
} 
1

それを試してみてください....

#import "bookdescriViewController.h" // import here your class name 

- (IBAction)backButton:(id)sender 
{ 
    bookdescriViewController *previosVC = [[bookdescriViewController alloc]init]; 
    [self.navigationController popViewControllerAnimated:YES]; // go to previous view controller 
    [self.navigationController popToRootViewControllerAnimated:YES]; // go to root view controller 
    [self.navigationController popToViewController:previosVC animated:YES]; // go to any view controller 
    [previosVC release]; 
} 
5

どの程度...

[self.navigationController dismissViewControllerAnimated:YES completion:NULL]; 

あなたは、ナビゲーションベースのコントローラに現在あると仮定すると、あなたがしたかったですナビゲーションベースのコントローラに入る前に、前のコントローラに戻ります。

17

あなたが戻って、以前のビューコントローラに移動したい場合は、実装する必要がありますメイン/ルートビューコントローラに移動

[self.navigationController popToRootViewControllerAnimated:YES]; 

以下の行を使用して、ビューコントローラ

[self.navigationController popToViewController:viewControllerObject animated:YES]; 
簡単にコピー&ペースト用
3

スウィフトソリューション:

navigationController?.popViewControllerAnimated(true) 
0

親ビューコントローラに戻り、現在のビューコントローラEX DEALLOC:

- (void)applicationDidEnterBackground:(NSNotification *)notification 
{ 
    NSInteger numberOfViewControllers = self.navigationController.viewControllers.count; 

    UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:numberOfViewControllers - 2]; 

    [self.navigationController popToViewController:vc animated:NO]; 
} 

または別のビューコントローラを

1

swift3、

@IBAction func back(_ sender: UIButton) { 
    self.dismiss(animated: true, completion: nil)  
} 
関連する問題