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
}
}
これにより、ビュー間で変数を渡すシングルトンアプローチを回避できます。これにより、シングルトンアプローチよりも長期的に優れた依存性注入モデルを簡単に実現できます。