Swift 5和Xcode 11
因此,在xCode 11中,窗口解决方案在appDelegate内部不再有效。他们将其移至SceneDelgate。您可以在SceneDelgate.swift文件中找到它。
您会发现它现在有var window: UIWindow?
礼物。
在我的情况下,我使用情节提要中的TabBarController并将其设置为rootViewController。
这是我的代码:
sceneDelegate.swift
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
self.window = self.window ?? UIWindow()//@JA- If this scene's self.window is nil then set a new UIWindow object to it.
//@Grab the storyboard and ensure that the tab bar controller is reinstantiated with the details below.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "tabBarController") as! UITabBarController
for child in tabBarController.viewControllers ?? [] {
if let top = child as? StateControllerProtocol {
print("State Controller Passed To:")
print(child.title!)
top.setState(state: stateController)
}
}
self.window!.rootViewController = tabBarController //Set the rootViewController to our modified version with the StateController instances
self.window!.makeKeyAndVisible()
print("Finished scene setting code")
guard let _ = (scene as? UIWindowScene) else { return }
}
确保像我在这里一样将其添加到正确的场景方法中。请注意,您将需要为故事板中使用的tabBarController或viewController 设置标识符名称。
就我而言,我这样做是为了设置stateController来跟踪选项卡视图之间的共享变量。如果您希望这样做,请添加以下代码...
StateController.swift
import Foundation
struct tdfvars{
var rbe:Double = 1.4
var t1half:Double = 1.5
var alphaBetaLate:Double = 3.0
var alphaBetaAcute:Double = 10.0
var totalDose:Double = 6000.00
var dosePerFraction:Double = 200.0
var numOfFractions:Double = 30
var totalTime:Double = 168
var ldrDose:Double = 8500.0
}
//@JA - Protocol that view controllers should have that defines that it should have a function to setState
protocol StateControllerProtocol {
func setState(state: StateController)
}
class StateController {
var tdfvariables:tdfvars = tdfvars()
}
注意:只需使用您自己的变量或您尝试跟踪的变量,我只是在tdfvariables结构中列出了我的示例。
在TabController的每个视图中,添加以下成员变量。
class SettingsViewController: UIViewController {
var stateController: StateController?
.... }
然后在这些相同的文件中添加以下内容:
extension SettingsViewController: StateControllerProtocol {
func setState(state: StateController) {
self.stateController = state
}
}
这样可以避免在视图之间传递变量的单例方法。这很容易实现依赖注入模型,从长远来看,它比单例方法好得多。