Home > database >  How can I access a custom variable in the SceneDelegate from the ViewController?
How can I access a custom variable in the SceneDelegate from the ViewController?

Time:05-16

I know how to access the scene delegate:

self.view.window.windowScene.delegate

And the window:

UIScene *scene = [[[[UIApplication sharedApplication] connectedScenes] allObjects] firstObject];

if ([scene.delegate conformsToProtocol:@protocol(UIWindowSceneDelegate)]) {
    
    UIWindow *window = [(id <UIWindowSceneDelegate>)scene.delegate window];
}

But both methods assume I haven't made any changes to the SceneDelegate.h/.m file.

I have created a custom toolbar and I don't know how to access it from the viewController:

SceneDelegate.h

#import <UIKit/UIKit.h>

@interface SceneDelegate : UIResponder <UIWindowSceneDelegate, NSToolbarDelegate>

@property (strong) NSToolbar *mainToolbar;

@property (strong, nonatomic) UIWindow * window;

@end

I'm using the NSToolbar because it's a Mac Catalyst app that can run on macOS also.

CodePudding user response:

Here's how you can check wether the object is kind of the desired class:

UIScene *scene = [[[[UIApplication sharedApplication] connectedScenes] allObjects] firstObject];
if ([scene.delegate isKindOfClass:SceneDelegate.class]) {
    SceneDelegate *delegate = (SceneDelegate *)scene.delegate;
    NSToolbar *toolbar = delegate.mainToolbar;
}

CodePudding user response:

But both methods assume I haven't made any changes to the SceneDelegate.h/.m file.

Why is this happening?

Because the code you have provided returns an instance of UIWindowSceneDelegate. But you are looking for the object of type SceneDelegate to access its interface. So you have to cast it to the desired interface.

How can I resolve this then?

So you can access it anywhere like:

SceneDelegate.shared.myCustomProperty

By defining a simple class method:

  (SceneDelegate *)shared {
    return (SceneDelegate *)UIApplication.sharedApplication.connectedScenes.allObjects.firstObject;
}

Note: Don't forget to import SceneDelegate.h and add the shared property to its interface.


You can also just cast it inline like (SceneDelegate *)self.view.window.windowScene.delegate

  • Related