2012-02-20 80 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)]; 

是回到层级的方法