Home > database >  iOS Convering an Old project to use UI
iOS Convering an Old project to use UI

Time:03-31

I am trying to convert (slowly mordenize) an old iOS project to support UISceneDelegate. However at the moment the old appdelegate creates a UIWindow and attaches a UIViewController to the windows rootViewController in the finishedLaunching.

As I am not ready to convert my application to Storyboards yet is there a way I can achieve the same result using UISceneDelegate ?

Please note the mainViewController is not backed by a XIB or a storyboard so there is no reference to any UI Files in the UIWindowSceneSessionRoleApplication portion of my info.plist

Regards Christian Stœr Andersen

CodePudding user response:

Implement the following method in your UIWindowSceneDelegate subclass to assign your UIViewController:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
   guard let windowScene = scene as? UIWindowScene else {return}
   self.window = UIWindow(windowScene: windowScene)
   self.window!.rootViewController = YourViewController() // Instantiate your UIViewController 
   self.window!.makeKeyAndVisible()
}

With objective-c:

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
   self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
   self.window.rootViewController = [[YourViewController alloc] init];
   [self.window makeKeyAndVisible];
}

CodePudding user response:

If you're developing Xamairn.iOS, here is the sample.

Remain the code in AppDelegate , and add the following code into SceneDelegate .

[Export ("scene:willConnectToSession:options:")]
public void WillConnect (UIScene scene, UISceneSession session, UISceneConnectionOptions connectionOptions)
{
    UIWindowScene windowScene = scene as UIWindowScene;
    Window = new UIWindow(windowScene);
    Window.Frame = windowScene.CoordinateSpace.Bounds;
    Window.RootViewController = new YourViewController();
    Window.MakeKeyAndVisible ();            
}
  • Related