Swift 5 & Xcode 11
Vì vậy, trong xCode 11, giải pháp cửa sổ không còn hợp lệ bên trong appDelegate. Họ đã chuyển cái này đến SceneDelgate. Bạn có thể tìm thấy điều này trong tập tin SceneDelgate.swift.
Bạn sẽ nhận thấy nó bây giờ có một var window: UIWindow?
món quà.
Trong tình huống của tôi, tôi đã sử dụng TabBarContoder từ bảng phân cảnh và muốn đặt nó làm rootViewControll.
Đây là mã của tôi:
cảnhDelegate.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 }
}
Hãy chắc chắn để thêm điều này vào phương pháp cảnh chính xác như tôi đã làm ở đây. Lưu ý rằng bạn sẽ cần đặt tên định danh cho tabBarContoder hoặc viewContoder mà bạn đang sử dụng trong bảng phân cảnh.

Trong trường hợp của tôi, tôi đã làm điều này để thiết lập một StateContoder để theo dõi các biến được chia sẻ giữa các chế độ xem tab. Nếu bạn muốn làm điều tương tự, hãy thêm đoạn mã sau ...
StateControll.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()
}
Lưu ý: Chỉ cần sử dụng các biến của riêng bạn hoặc bất cứ điều gì bạn đang cố gắng theo dõi thay vào đó, tôi chỉ liệt kê của tôi làm ví dụ trong cấu trúc tdfvariables.
Trong mỗi chế độ xem của TabContoder, hãy thêm biến thành viên sau.
class SettingsViewController: UIViewController {
var stateController: StateController?
.... }
Sau đó, trong các tệp tương tự thêm vào như sau:
extension SettingsViewController: StateControllerProtocol {
func setState(state: StateController) {
self.stateController = state
}
}
Điều này không cho phép bạn tránh cách tiếp cận đơn lẻ để chuyển các biến giữa các khung nhìn. Điều này cho phép dễ dàng cho mô hình tiêm phụ thuộc, lâu dài hơn phương pháp đơn lẻ.