【问题标题】:How to change destination of back button to another storyboard iOS?如何将后退按钮的目的地更改为另一个故事板 iOS?
【发布时间】:2015-12-18 10:14:25
【问题描述】:

我有一个流程如下的应用程序:

  1. 主情节提要:导航 A -> ViewController A1(我的应用程序的第一个屏幕)...转到某些屏幕并转到其他情节提要。

  2. 第二个故事板:导航 B -> ViewController B1 -> ViewController B2 -> ViewController B3。

我在B3时的正常逻辑,点击返回按钮回到B2并再次点击返回B1 ...
现在我想通过单击返回按钮从 B3 转到 A1。我使用此代码

UIStoryboard *Main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];


UIViewController *theTabBar = (UIViewController *)[Main instantiateViewControllerWithIdentifier:@"PageController"];

[self.navigationController pushViewController:theTabBar animated:YES];

但它没有按我的意愿工作(在 Main 上出现了后退按钮,这是错误的),我将 pushViewController 更改为 popViewController 但它也不起作用。

【问题讨论】:

  • 不要做这样的事情,它会让你的最终用户感到困惑,如果导航不可预测,你也可能会冒着通过审核程序失败的风险。
  • 从第二个故事板中移除导航 B 并直接从第一个导航控制器推送视图控制器 B1。
  • @holex 谢谢你,但我没有选择,这是我客户的理想选择。

标签: ios objective-c iphone back


【解决方案1】:

在 b3 的后退按钮上正好这个代码:-

[[NSNotificationCenter defaultCenter] postNotificationName:@"takeBack" object:nil];

并在 A1 的视图 viewwillappear 上编写此代码:-

-(void)viewWillAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popping) name:@"takeBack" object:nil];
}
-(void)popping{
    NSUInteger index=-1;

    for (UIViewController *view in [self.navigationController viewControllers]) {
        if ([view isKindOfClass:[self class]]) {
            index=[[self.navigationController viewControllers] indexOfObject:view];
        }
    }

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

}

【讨论】:

  • 谢谢,但它不起作用。我没有看到任何事情发生,我有一个问题,为什么 index = -1?
  • 它工作正常,我已经检查过了。我们已将索引初始化为 -1,因为 -1 不是任何控制器的索引。
  • 你能分享我的测试项目吗?谢谢。
  • 我的项目没有按照你的方式推动控制器。我使用了故事板dropbox.com/s/n486bcbfx64ekin/storyboard.png?dl=0。也许它对我不起作用。你有别的办法吗?
  • 我用另一种方式解决了我的问题,但我使用了你的一些代码。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-04
  • 2011-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多