스위프트 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
}
}
이것이하는 일은 뷰 사이에 변수를 전달 하는 단일 방법 을 피할 수있게하는 것 입니다. 이것은 싱글 톤 방식보다 훨씬 더 긴 의존성 주입 모델을 쉽게 허용합니다.