【问题标题】:Back button action of UINavigation Views ?UINavigation 视图的后退按钮操作?
【发布时间】:2013-08-05 09:30:58
【问题描述】:

我有 4 个视图。

第一个是rootView,然后是View1View2View3
它正在根据rootView-> View1-> View2-> View3 工作。

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

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

由于索引不起作用,此时当我从视图 1 推送视图 3 时,视图 3 的索引值为 2。

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

【问题讨论】:

  • 不清楚你想从 View1 跳转到 View3 需要 push 或 pop View2 from View3 需要弹出。

标签: ios objective-c uinavigationcontroller


【解决方案1】:

initWithBackButton:(UIBarButtonItem *)backButton;创建新类MYViewController : UIViewController

@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:^{}]; 
    }];

}

【讨论】:

    【解决方案2】:

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

    UIViewController *view1Reference = [[UIViewController alloc] init];
    [self.navigationController popToViewController:view1Reference animated:YES];
    

    【讨论】:

    • 你在用故事板吗?
    • 然后使用自定义的后退按钮,从该按钮调用一个动作,并在该动作中使用此代码..
    • 好的,然后将 UIButton 添加到导航栏并调用一个操作,在该操作中使用此代码.. UIViewController *view1Reference = [[UIViewController alloc] init]; [self.navigationController popToViewController:view1Reference Animation:YES];
    【解决方案3】:

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

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

    (上一个视图)

    [self.navigationController popViewControllerAnimated:YES];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 2013-09-25
      • 1970-01-01
      • 2012-12-24
      • 2021-11-12
      • 1970-01-01
      相关资源
      最近更新 更多