【问题标题】:How to open a modal with UITabBarController and on close, display last tab bar view?如何使用 UITabBarController 打开模式并在关闭时显示最后一个标签栏视图?
【发布时间】:2015-09-12 05:53:17
【问题描述】:

所以我在一个有 3 个按钮的故事板上有一个 UITabBarController。其中两个通过标准视图关联segues打开与标签栏关联的标准视图......第三项是问题。

我想在模态视图中打开位于另一个故事板(由应用程序中的其他视图共享)中的表单,并在关闭或提交时返回到选项卡式视图中先前处于活动状态的任何选项卡。

我已经看到了一些“选项”,但它们似乎都不是我想要做的。

有什么想法吗?

编辑:它是由一个 mod 从我原来的问题中编辑出来的,但我是用 Swift 编写的……而不是 Obj-C。

【问题讨论】:

    标签: ios swift cocoa-touch uitabbarcontroller uistoryboard


    【解决方案1】:

    所以我有一个潜在的解决方案,可以使用空白视图控制器、shouldSelectViewController 和 UITabBarController Delegate。唯一的问题是,我对它的脆弱程度不是特别满意。

    func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
    
        // This is a bit dangerous if we move where 'newController' is located in the tabs, this will break.
        let newController = viewControllers![1] as! UIViewController
    
        // Check if the view about to load is the second tab and if it is, load the modal form instead.
        if viewController == newController {
            let storyboard = UIStoryboard(name: "ModalController", bundle: nil)
            let vc = storyboard.instantiateInitialViewController() as? UIViewController
    
            presentViewController(vc!, animated: true, completion: nil)
    
            return false
        } else {
            return true
        }
    }
    

    这很好用,但如果有人过来并重新安排故事板中选项卡的顺序,这会中断。

    另外,为了给@rmp 一些信任,另一个选项是“如果您为构成 tabBarController 中的选项卡的所有根视图控制器提供一个标题,那么您可以使用 viewController.title 来检查哪个选项卡在shouldSelectViewController 委托。当然,您仍然冒着有人重命名您的视图标题属性的风险。”

    【讨论】:

    • 避免移动标签崩溃的更好方法是使用这个: let newController = { viewControllers!.filter { $0 is MyTabViewControllerSubclass }.first! }()
    【解决方案2】:

    回答你关于它“易碎”的第二个问题

    如果您为构成tabBarController 中的选项卡的所有根视图控制器提供标题,那么您可以使用 viewController.title 来检查在shouldSelectViewController 委托中点击了哪个选项卡。当然,您仍然冒着有人重命名您的 views 标题属性的风险。

    【讨论】:

    • 对...我想这样的事情无法完全得到保护。
    • 另外......你真的不必为每个根视图控制器都设置一个标题......只要你正在检查的那个。
    • @kevindeleon 是的,我只是想保持一致;)
    【解决方案3】:

    我也模拟过,参考链接:-https://github.com/deepesh259nitk/MultipleStoryBoards/tree/master

    1. 运行项目
    2. 点击 Tab2
    3. 您会看到 3 个按钮
    4. 点击按钮3

    【讨论】:

      【解决方案4】:

      听起来您并不真正想要一个选项卡,选项卡旨在包含一个视图,而不是呈现一个新的/模态视图。但是,在某些情况下,您想做的事情是有意义的,例如某些应用程序将使用中心选项卡作为按钮来添加新项目或执行某些操作(Yelp 就是一个很好的例子)。在这些情况下,选项卡通常更大或在视觉上有所不同,以表明它不是真正的选项卡。这就是我认为你真正追求的。

      为此,您需要:

      1. 在tabBar 中创建一个标签,这将只是一个占位符
      2. 创建一个图标或图像以用作将在占位符选项卡上的按钮
      3. 使用tabBarController 类的viewWillLayoutSubviews 将按钮放在占位符选项卡上。见下文(纵向示例代码)。
      4. 向按钮添加一个动作以显示您的模态视图。

      注意:您可能需要使用以下代码调整按钮的位置以满足您的需要。

      -(void)viewWillLayoutSubviews{
      [super viewWillLayoutSubviews];
      
                  if (UIDeviceOrientationIsPortrait && !centerTabButton) {
      
                      //CUSTOM CENTER TAB BAR BUTTON
                      UIImage *buttonImage = [UIImage imageNamed:@"icn_CenterTabBarIcon.png"];
                      UIImage *highlightImage = [UIImage imageNamed:@"icn_CenterTabBarIcon.png"];
      
                      centerTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
                      centerTabButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
                      [centerTabButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
                      [centerTabButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
                      [centerTabButton setShowsTouchWhenHighlighted:FALSE];
                      [centerTabButton setAdjustsImageWhenHighlighted:FALSE];
                      [centerTabButton addTarget:self action:@selector(centerTabClicked:) forControlEvents:UIControlEventTouchUpInside];
      
                      CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
      
                      //center the button on the tab bar based on the tab bar's height
                      if (heightDifference < 0){
                          centerTabButton.center = self.tabBar.center;
                      }
                      else{
                          CGPoint center = self.tabBar.center;
                          center.y = center.y - heightDifference/2.0;
                          centerTabButton.center = center;
                      }
      
                      [self.view addSubview:centerTabButton];
                  }
              }
      

      【讨论】:

      • 如帖子中的标签所示,我使用的是 Swift,而不是 Obj-C ......但我明白你在做什么。我希望能够通过自动布局将内容保存在故事板中,而不是以编程方式构建内容,这仅仅是因为 Apple 制定了前进的道路。我想知道是否可以使用 UITabBarController Delegate 拦截标签点击。
      • 是的,这段代码只是从现有的实时应用程序的副本,翻译成 swift 应该很简单。是的,您可以覆盖tabBarController:shouldSelectViewController:,然后使用其他一些自定义代码来呈现模式,但您仍然需要在情节提要之外做一些工作。
      • 在上面查看我更新的代码......它可以工作,但我对它的脆弱性有点不满意......有什么想法吗?
      • @rmp 如何在 tabBar 中创建占位符标签?如果我创建 tabBar 项,那么我必须为此分配一个 Viewcontroller,对吧?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 2022-12-14
      相关资源
      最近更新 更多