2012-02-20 78 views
1

我有UIViewCOntroller,我的代碼如下。單擊後退按鈕時返回 - 初學者

TviewController *tviewController = [[TviewController alloc]init]; 
    [self.navigationController pushViewController:tviewController animated:YES]; 

現在從TviewController我去另一個viewCOntroller;

XviewController *xviewController = [[XviewController alloc]init]; 
    [self.navigationController pushViewController:xviewController animated:YES]; 
在此 XviewController

有一個按鈕,當我點擊該按鈕,我需要移動BACKTviewController我如何做到這一點編程?

注意:我不想使用pushViewController並進一步推送。我需要回到TviewController(在兩次單擊後退按鈕)

回答

1

有3種可能的方式。

  1. 使用popToRootViewControllerAnimated: - >回到根視圖控制器(第一視圖控制器)

  2. 使用popViewControllerAnimated: - >去向後1.它的確切相同的方式,後退按鈕。

  3. 使用popToViewController:animated: - >回到UIViewController你想要的(只要它在後面堆疊)。

點1 & 2是很容易實現和其他答案引導你到那裏。

爲3點,那就是:

@class TviewController.h; 
@interface XviewController : UIViewController 
{ 
//this is XviewController.h 
//you may use `#import` other than use `@class` but on some reason you can't, if you use`#import XviewController.h` in your TviewController class. 
} 

//XviewController.m 
#import TviewController.h 
#import XviewController.h 
@implementation 

-(IBAction) backTviewController 
{ 
    for (UIViewController *viewController in self.navigationController.viewControllers) 
     if ([viewController isMemberOfClass:[TviewController class]]{ 
     [self.navigationController popToViewController:viewController animated:YES]; 
     break; 
     } 
} 
+0

所以,如果我是用第三個方法我應該使用'@ class',而不是'import'? – Illep 2012-02-21 16:01:28

+0

是的,更安全。您可以在.h中使用'@ class',並在.m中添加'#import'。或者在.h中使用'#import'。任何人都應該工作。很難解釋爲什麼,這是另一個話題和答案的另一個問題:)。 – HelmiB 2012-02-21 16:52:31

+0

感謝+1對你:) – Illep 2012-02-22 13:38:17

3

只是

[self.navigationController popViewControllerAnimated:YES]; 

你應該花一些時間閱讀guidelines約視圖控制器...

3
[self.navigationController popViewControllerAnimated:BOOL)] 
    [self.navigationController popToViewController:(UIViewController *) animated:(BOOL)]; 

     [self.navigationController popToRootViewControllerAnimated:(BOOL)]; 

是回到層級的方法