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

时间:2022-11-21 21:25:41

I have a UIViewCOntroller, and my code is as follows.

我有一个UIViewCOntroller,我的代码如下。

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

Now from TviewController i go to another viewCOntroller;

现在从TviewController我去另一个viewCOntroller;

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

in this XviewController there is a button, when i click that button i need to move BACK to the TviewController How do i do this programatically ?

在这个XviewController中有一个按钮,当我点击该按钮时我需要将BACK移动到TviewController我如何以编程方式执行此操作?

note: I don't want to use pushViewController and push it further. I need to go back to the TviewController (as in clicking the back button twice)

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

3 个解决方案

#1


1  

there is 3 possible ways.

有3种可能的方法。

  1. use popToRootViewControllerAnimated: -> to go back root view controller (first view controller)

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

  2. use popViewControllerAnimated: -> to go backward 1. it's exact same way as back button.

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

  3. use popToViewController:animated: -> to go back to UIViewController you want (as long as it's stack at back).

    使用popToViewController:animated: - >返回你想要的UIViewController(只要它在后面的堆栈)。

point 1 & 2 is pretty easy to implement and other answers lead you to there.

第1和第2点很容易实现,其他答案会引导您到达那里。

for point 3, here it is:

对于第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;
      } 
}

#2


3  

Just

[self.navigationController popViewControllerAnimated:YES];

You should spend some time reading the guidelines about view controllers...

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

#3


3  

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

        [self.navigationController popToRootViewControllerAnimated:(BOOL)];

are methods to go back in hierarchy

是在层次结构中返回的方法

#1


1  

there is 3 possible ways.

有3种可能的方法。

  1. use popToRootViewControllerAnimated: -> to go back root view controller (first view controller)

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

  2. use popViewControllerAnimated: -> to go backward 1. it's exact same way as back button.

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

  3. use popToViewController:animated: -> to go back to UIViewController you want (as long as it's stack at back).

    使用popToViewController:animated: - >返回你想要的UIViewController(只要它在后面的堆栈)。

point 1 & 2 is pretty easy to implement and other answers lead you to there.

第1和第2点很容易实现,其他答案会引导您到达那里。

for point 3, here it is:

对于第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;
      } 
}

#2


3  

Just

[self.navigationController popViewControllerAnimated:YES];

You should spend some time reading the guidelines about view controllers...

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

#3


3  

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

        [self.navigationController popToRootViewControllerAnimated:(BOOL)];

are methods to go back in hierarchy

是在层次结构中返回的方法