如何在没有链接到它的View Controller的情况下在swift上创建标签栏项目

问题描述 投票:4回答:3

所以我的问题是我正在创建一个没有故事板的UITabBarController,而我却不知道如何在没有Viewcontroller的情况下创建UITabBar项目。因为我想要做的是我的UITabBarItem的第二项是一个没有呈现视图控制器的动作。

ios swift uitabbarcontroller uitabbaritem
3个回答
3
投票

虽然我使用故事板,但我在应用程序(关闭按钮)中实现了类似的功能。关闭按钮是一个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")   
    }
}

tab-bar close button


1
投票

有一种符合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;
}

0
投票

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

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

}
© www.soinside.com 2019 - 2024. All rights reserved.