swift 在XCode故事板模式下工作时,在iOS 13+中定义状态的最佳方法是什么?[closed]

5f0d552i  于 2023-01-01  发布在  Swift
关注(0)|答案(1)|浏览(121)

已关闭。此问题为opinion-based。当前不接受答案。
**想要改进此问题吗?**请更新此问题,以便editing this post可以用事实和引文来回答。

昨天关门了。
Improve this question
我找到的每个答案都是这样的:SwiftUI使用@state变量来管理应用程序中的状态。我的应用程序在XCode中处于故事板模式。我是否要向AppDelegate添加一个状态枚举,以便每个其他ViewController都可以更新和访问它?
我试图在这个问题中搜索最佳实践,希望得到一个小代码片段,显示一些有用的用户状态以及如何在Swift5.0中获得它们,但似乎SwiftUI已经使这个主题超载。

9gm1akwq

9gm1akwq1#

var window: UIWindow?
    
    //MARK: Set Tabbar and view controllers
    let tabBar                = UITabBarController()
    let firstViewController   = UIViewController()
    let secondViewController  = UIViewController()
    let thirdViewController   = UIViewController()
    
    func addingViewControllerToTabBarController() {
        //MARK: Adding ViewControllers to navigation controllers,
        let firstNavigationController = UINavigationController(rootViewController: firstViewController)
        let secondNavigationController = UINavigationController(rootViewController: secondViewController)
        let thirdNavigationController = UINavigationController(rootViewController: thirdViewController)
        
        //MARK: set view controllers to tabbar according to the order
        tabBar.setViewControllers([firstNavigationController, secondNavigationController, thirdNavigationController], animated: true)
    }
    
    //MARK: Scene delegate function.
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        
        //MARK: Programmatically adding tabbar to the SceneDelegate
        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(windowScene: windowScene)
        window?.rootViewController = tabBar
        window?.backgroundColor = .systemBackground
        window?.makeKeyAndVisible()
        
        //MARK: Set titles to related controllers
        firstViewController.title = "firstController"
        secondViewController.title = "secondController"
        thirdViewController.title = "thirdController"
        
        //MARK: UITabbar appearances in selected and unselected conditions
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.label], for: UIControl.State.selected)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.systemGray], for: UIControl.State.normal)
        
        //MARK: call function
        addingViewControllerToTabBarController()
    }

相关问题