0

我有 4 个视图。

第一个是rootView然后View1, View2& View3
它正在按照rootView-> View1-> View2-> View3.

我必须从 View1 跳到 View3但是当我使用 View3 的后退按钮时,它会弹出到 View2 。如果我使用以下代码

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];

它不起作用的原因是索引,此时当我从视图 1 推送视图 3 时,视图 3 的索引值为 2。

那么还有其他方法可以使用后退按钮从 View3 中弹出 View2 吗?

4

3 回答 3

0

创建新MYViewController : UIViewControllerinitWithBackButton:(UIBarButtonItem *)backButton;

@property (nonatomic, strong) UIBarButtonItem *saveBackButton

- (id)initWithBackButton:(UIBarButtonItem *)backButton {

  self = [super init];

  if(self){
      self.saveBackButton = backButton;
  }
  return self;
}

在根

创建3个属性vc1、vc2、vc3;

当你创建MYViewController

{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, width, height);
    [button addTarget:self action:@selector(iWantToOpenNumber2:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    self.vc3 = [[MYViewController alloc] initWithBackButton:backItem];
}

- (void)iWantToOpenNumber2:(id)sender {
    [self dismissViewControllerAnimated: YES completion: ^{
       [self presentViewController:self.vc2 animated: YES completion:^{}]; 
    }];

}
于 2013-08-05T13:06:54.730 回答
0

在 view3 中导入 view1,然后为 view1 创建一个实例并粘贴此代码

UIViewController *view1Reference = [[UIViewController alloc] init];
[self.navigationController popToViewController:view1Reference animated:YES];
于 2013-08-05T09:44:51.850 回答
0

通过后退按钮。(具体指标)

NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:2] animated:NO];

(对于以前的看法)

[self.navigationController popViewControllerAnimated:YES];
于 2013-08-05T10:11:00.947 回答