स्विफ्ट 5 और Xcode 11
तो xCode 11 में विंडो समाधान appDelegate के अंदर मान्य नहीं है। वे इसे सीनेलगेट ले गए। आप इसे 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.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()
}
नोट: बस अपने स्वयं के चरों का उपयोग करें या जो भी आप इसके बजाय ट्रैक रखने की कोशिश कर रहे हैं, मैंने सिर्फ एक उदाहरण के रूप में tdfvitables संरचना में मेरा सूचीबद्ध किया है।
TabController के प्रत्येक दृश्य में निम्न सदस्य चर जोड़ें।
class SettingsViewController: UIViewController {
var stateController: StateController?
.... }
फिर उन्हीं फाइलों में निम्नलिखित जोड़ते हैं:
extension SettingsViewController: StateControllerProtocol {
func setState(state: StateController) {
self.stateController = state
}
}
यह क्या करता है आप विचारों के बीच चर चर के लिए एकल दृष्टिकोण से बचने के लिए अनुमति देता है । यह निर्भरता इंजेक्शन मॉडल के लिए आसानी से अनुमति देता है जो कि लंबे समय तक बेहतर है फिर सिंगलटन दृष्टिकोण।