Skip to main content
deleted 12 characters in body
Source Link
slushy
  • 12.2k
  • 3
  • 43
  • 55

Xcode 13+, iOSiOS 13+, Swift

Scenes represent instances of the app's UI and each scene maintains its own, fully-independent, state. And the app itself, which is never more than a single instance, maintains references to all of these connected scenes. And scenes are classes so they are reference-types. Therefore, simply maintain a reference to the scene itself when it is connected and then find that scene in the set of connected scenes within UIApplication.shared.

// Create a globally-accessible variable of some kind (global
// variable, static property, property of a singleton, etc.) that
// references this current scene (itself).
var currentScene: UIScene?

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = scene as? UIWindowScene else {
            return
        }
        // Save the reference when the scene is born.
        currentScene = scene
    }
    
    func borat() {
        print("great success")
    }
}

// Here is a convenient view controller extension.
extension UIViewController {
    var sceneDelegate: SceneDelegate? {
        for scene in UIApplication.shared.connectedScenes {
            if scene == currentScene,
               let delegate = scene.delegate as? SceneDelegate {
                return delegate
            }
        }
        return nil
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        sceneDelegate?.borat() // "great success"
    }
}

Xcode 13+, iOS 13+, Swift

Scenes represent instances of the app's UI and each scene maintains its own, fully-independent, state. And the app itself, which is never more than a single instance, maintains references to all of these connected scenes. And scenes are classes so they are reference-types. Therefore, simply maintain a reference to the scene itself when it is connected and then find that scene in the set of connected scenes within UIApplication.shared.

// Create a globally-accessible variable of some kind (global
// variable, static property, property of a singleton, etc.) that
// references this current scene (itself).
var currentScene: UIScene?

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = scene as? UIWindowScene else {
            return
        }
        // Save the reference when the scene is born.
        currentScene = scene
    }
    
    func borat() {
        print("great success")
    }
}

// Here is a convenient view controller extension.
extension UIViewController {
    var sceneDelegate: SceneDelegate? {
        for scene in UIApplication.shared.connectedScenes {
            if scene == currentScene,
               let delegate = scene.delegate as? SceneDelegate {
                return delegate
            }
        }
        return nil
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        sceneDelegate?.borat() // "great success"
    }
}

iOS 13+, Swift

Scenes represent instances of the app's UI and each scene maintains its own, fully-independent state. And the app itself, which is never more than a single instance, maintains references to all of these connected scenes. And scenes are classes so they are reference-types. Therefore, simply maintain a reference to the scene itself when it is connected and then find that scene in the set of connected scenes within UIApplication.shared.

// Create a globally-accessible variable of some kind (global
// variable, static property, property of a singleton, etc.) that
// references this current scene (itself).
var currentScene: UIScene?

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = scene as? UIWindowScene else {
            return
        }
        // Save the reference when the scene is born.
        currentScene = scene
    }
    
    func borat() {
        print("great success")
    }
}

// Here is a convenient view controller extension.
extension UIViewController {
    var sceneDelegate: SceneDelegate? {
        for scene in UIApplication.shared.connectedScenes {
            if scene == currentScene,
               let delegate = scene.delegate as? SceneDelegate {
                return delegate
            }
        }
        return nil
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        sceneDelegate?.borat() // "great success"
    }
}
deleted 68 characters in body
Source Link
slushy
  • 12.2k
  • 3
  • 43
  • 55

Xcode 13+, iOS 13+, Swift

Scenes represent instances of the app's UI and each scene maintains its own, fully-independent, state. And the app itself, which is never more than a single instance, maintains references to all of these connected scenes. And scenes are classes so they are reference-types. Therefore, simply maintain a reference to the scene itself within the scene beforewhen it is connected to a window and then find that scene in the set of connected scenes within UIApplication.shared.

// Create a globally-accessible variable of some kind (global
// variable, static property, property of a singleton, etc.) that
// references the scene of this current "UIscene instance"(itself).
var currentScene: UIScene?

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = scene as? UIWindowScene else {
            return
        }
        // StoreSave the reference when the scene is born.
        currentScene = scene
    }
    
    func borat() {
        print("great success")
    }
}

// As a convenience, considerHere extendingis a ubiquitousconvenient view object
// to find the delegate on itscontroller ownextension.
extension UIViewController {
    var sceneDelegate: SceneDelegate? {
        for scene in UIApplication.shared.connectedScenes {
            if scene == currentScene,
               let delegate = scene.delegate as? SceneDelegate {
                return delegate
            }
        }
        return nil
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        sceneDelegate?.borat() // "great success"
    }
}

Xcode 13+, iOS 13+, Swift

Scenes represent instances of the app's UI and each scene maintains its own, fully-independent, state. And the app itself, which is never more than a single instance, maintains references to all of these connected scenes. And scenes are classes so they are reference-types. Therefore, simply maintain a reference to the scene itself within the scene before it is connected to a window and then find that scene in the set of connected scenes within UIApplication.shared.

// Create a globally-accessible variable of some kind (global
// variable, static property, property of a singleton, etc.) that
// references the scene of this current "UI instance".
var currentScene: UIScene?

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = scene as? UIWindowScene else {
            return
        }
        // Store the reference when the scene is born.
        currentScene = scene
    }
    
    func borat() {
        print("great success")
    }
}

// As a convenience, consider extending a ubiquitous view object
// to find the delegate on its own.
extension UIViewController {
    var sceneDelegate: SceneDelegate? {
        for scene in UIApplication.shared.connectedScenes {
            if scene == currentScene,
               let delegate = scene.delegate as? SceneDelegate {
                return delegate
            }
        }
        return nil
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        sceneDelegate?.borat() // "great success"
    }
}

Xcode 13+, iOS 13+, Swift

Scenes represent instances of the app's UI and each scene maintains its own, fully-independent, state. And the app itself, which is never more than a single instance, maintains references to all of these connected scenes. And scenes are classes so they are reference-types. Therefore, simply maintain a reference to the scene itself when it is connected and then find that scene in the set of connected scenes within UIApplication.shared.

// Create a globally-accessible variable of some kind (global
// variable, static property, property of a singleton, etc.) that
// references this current scene (itself).
var currentScene: UIScene?

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = scene as? UIWindowScene else {
            return
        }
        // Save the reference when the scene is born.
        currentScene = scene
    }
    
    func borat() {
        print("great success")
    }
}

// Here is a convenient view controller extension.
extension UIViewController {
    var sceneDelegate: SceneDelegate? {
        for scene in UIApplication.shared.connectedScenes {
            if scene == currentScene,
               let delegate = scene.delegate as? SceneDelegate {
                return delegate
            }
        }
        return nil
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        sceneDelegate?.borat() // "great success"
    }
}
deleted 4 characters in body
Source Link
slushy
  • 12.2k
  • 3
  • 43
  • 55

Xcode 13+, iOS 13+, Swift

Scenes represent instances of the app's UI and each scene maintains its own, fully-independent, state. And the app itself, which is never more than a single instance, maintains references to all of these connected scenes. And scenes are classes so they are reference-types. Therefore, simply maintain a reference to the scene itself within the scene before it is connected to a window and then find that scene in the set of connected scenes within UIApplication.shared.

// Create a globally-accessible variable of some kind (global variable,
// variable, static property, property of a singleton, etc.) that references
// references the scene of this current "UI instance".
var currentScene: UIScene?

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = scene as? UIWindowScene else {
            return
        }
        currentScene = scene // RecordStore the reference when the scene is born.
        currentScene = scene
    }
    
    func borat() {
        print("great success")
    }
}

// As a convenience, I've extendedconsider UIViewControllerextending toa performubiquitous theview taskobject
// ofto findingfind the delegate so that it can be gotten from any view
// controller inon theits appown.
extension UIViewController {
    var sceneDelegate: SceneDelegate? {
        for scene in UIApplication.shared.connectedScenes {
            if scene == currentScene,
               let delegate = scene.delegate as? SceneDelegate {
                return delegate
            }
        }
        return nil
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        sceneDelegate?.borat() // "great success"
    }
}

Xcode 13+, iOS 13+, Swift

Scenes represent instances of the app's UI and each scene maintains its own, fully-independent, state. And the app itself, which is never more than a single instance, maintains references to all of these connected scenes. Therefore, simply maintain a reference to the scene itself within the scene before it is connected to a window and then find that scene in the set of connected scenes within UIApplication.shared.

// Create a globally-accessible variable of some kind (global variable,
// static property, property of a singleton, etc.) that references
// the scene of this current "UI instance".
var currentScene: UIScene?

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = scene as? UIWindowScene else {
            return
        }
        currentScene = scene // Record the reference when the scene is born.
    }
    
    func borat() {
        print("great success")
    }
}

// As a convenience, I've extended UIViewController to perform the task
// of finding the delegate so that it can be gotten from any view
// controller in the app.
extension UIViewController {
    var sceneDelegate: SceneDelegate? {
        for scene in UIApplication.shared.connectedScenes {
            if scene == currentScene,
               let delegate = scene.delegate as? SceneDelegate {
                return delegate
            }
        }
        return nil
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        sceneDelegate?.borat() // "great success"
    }
}

Xcode 13+, iOS 13+, Swift

Scenes represent instances of the app's UI and each scene maintains its own, fully-independent, state. And the app itself, which is never more than a single instance, maintains references to all of these connected scenes. And scenes are classes so they are reference-types. Therefore, simply maintain a reference to the scene itself within the scene before it is connected to a window and then find that scene in the set of connected scenes within UIApplication.shared.

// Create a globally-accessible variable of some kind (global
// variable, static property, property of a singleton, etc.) that
// references the scene of this current "UI instance".
var currentScene: UIScene?

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = scene as? UIWindowScene else {
            return
        }
        // Store the reference when the scene is born.
        currentScene = scene
    }
    
    func borat() {
        print("great success")
    }
}

// As a convenience, consider extending a ubiquitous view object
// to find the delegate on its own.
extension UIViewController {
    var sceneDelegate: SceneDelegate? {
        for scene in UIApplication.shared.connectedScenes {
            if scene == currentScene,
               let delegate = scene.delegate as? SceneDelegate {
                return delegate
            }
        }
        return nil
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        sceneDelegate?.borat() // "great success"
    }
}
added 9 characters in body
Source Link
slushy
  • 12.2k
  • 3
  • 43
  • 55
Loading
deleted 273 characters in body
Source Link
slushy
  • 12.2k
  • 3
  • 43
  • 55
Loading
added 15 characters in body
Source Link
slushy
  • 12.2k
  • 3
  • 43
  • 55
Loading
added 3 characters in body
Source Link
slushy
  • 12.2k
  • 3
  • 43
  • 55
Loading
Source Link
slushy
  • 12.2k
  • 3
  • 43
  • 55
Loading