单击后退按钮时返回 - 初学者

时间:2012-02-20 18:48:06

标签: iphone objective-c

我有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中有一个按钮,当我点击该按钮时,我需要将 BACK 移动到TviewController我如何以编程方式执行此操作?

注意:我不想使用pushViewController并进一步推送它。我需要回到TviewController(就像点击两次后退按钮一样)

3 个答案:

答案 0 :(得分:3)

只需

[self.navigationController popViewControllerAnimated:YES];

您应该花一些时间阅读有关视图控制器的guidelines ...

答案 1 :(得分:3)

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

        [self.navigationController popToRootViewControllerAnimated:(BOOL)];

是返回层次结构的方法

答案 2 :(得分:1)

有三种可能的方法。

  1. 使用popToRootViewControllerAnimated: - >返回根视图控制器(第一个视图控制器)

  2. 使用popViewControllerAnimated: - >向后走1.它与后退按钮完全相同。

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

  4. 第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;
          } 
    }