【问题标题】:How to create uitabbar item on swift without a View Controller lined to it如何在没有链接到它的视图控制器的情况下快速创建标签栏项目
【发布时间】:2018-01-29 15:25:17
【问题描述】:

所以我的问题是我正在创建一个没有情节提要的 UITabBarController,并且我对如何在没有 Viewcontroller 的情况下创建一个 UITabBar 项目感到困惑。因为我想做的是我的 UITabBarItem 的第二项是一个不呈现视图控制器的动作。

【问题讨论】:

  • 使用标签栏界面,但让一个标签转到新视图,这可能会让用户非常困惑。但是,如果您想这样做,您应该查看UITabBarControllerDelegate 和/或UITabBarDelegate

标签: ios swift uitabbarcontroller uitabbaritem


【解决方案1】:

尽管我使用故事板,但我在应用程序中实现了类似的功能(关闭按钮)。关闭按钮是一个 viewController,但我使用以下代码让它像普通按钮一样工作:

 func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    // this provides UI feedback that the button has been pressed, even though it leads to the dismissal
        if viewController == self.viewControllers![4] {

           viewController.tabBarItem.image? = UIImage(named: "TabBarClose")!.imageWithColor(UIColor.red).withRenderingMode(UIImageRenderingMode.alwaysOriginal)

            return false
        } else {
        return true
        }
    }

override func viewDidDisappear(_ animated: Bool) {
//sets the close button back to original image
    self.viewControllers![4].tabBarItem.image = UIImage(named: "TabBarClose")!
}

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
// this is so it never actually goes to the close buttons viewController
    currentTab = self.selectedIndex != 4 ? self.selectedIndex:currentTab
    saveTabIndexToPreferences(currentTab!)

}

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    // assign functionality to the close button
    if item.tag == 100 {// this is old code, there is probably a better way to reference the tab
        dismissTabBar()
    } else {

    }        
}

编辑(针对被问到的问题)

func selectTabIndexFromPreferences() {
    let defaults:UserDefaults = UserDefaults.standard
    selectedIndex = defaults.integer(forKey: "songSelectionTabIndex_preference")
}

func saveTabIndexToPreferences(_ index:Int?) {
    if index != nil {
        let defaults:UserDefaults = UserDefaults.standard
        defaults.set(index!, forKey: "songSelectionTabIndex_preference")   
    }
}

【讨论】:

  • 谁声明 currenttab 和 saveTabIndexToPreferences
  • currentTab 只是一个局部变量,我会在答案中添加其他函数
【解决方案2】:

如果你真的想这样做(这是非常不标准的 UI...),那么你可以添加一个空视图控制器,但在你的标签栏委托实现

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {

}

【讨论】:

    【解决方案3】:

    有一种符合 API 的方法可以做到这一点。

    UITabBarControllerDelegate 有一个可以实现的shouldSelect 方法:https://developer.apple.com/documentation/uikit/uitabbarcontrollerdelegate/1621166-tabbarcontroller

    类似的东西:

    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
      if (viewController == self.centerViewController) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Test" message:@"This is a test" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
        [alert addAction:defaultAction];
        [tabBarController presentViewController:alert animated:YES completion:nil];
    
        return NO;
      }
    
      return YES;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多