Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

abhimohata

macrumors newbie
Original poster
Oct 11, 2022
1
0
I am building an iOS application(should also run on iPadOS and tvOS). We want to build without using a storyboard file and hence, UI related things would be done programmatically.

While running the application on iOS 16 simulator, I get the following exception :

IOSSceneDelegate[24955:574895] Unbalanced calls to begin/end appearance transitions for <UINavigationController: 0x10a80bc00>.



Creation of UIWindow and assigning the rootViewController is done through code.

I want to write the code for window creation in a separate class and then invoke the same from SceneDelegate. When this is done, the said exception of 'Unbalanced calls...' comes during app launch.



My Window Creation Code:

Swift:
import Foundation

import UIKit

class MyWindow: NSObject {

    var window: UIWindow!

    var navcontrol: UINavigationController!

    func CreateWindow () {

      
        if #available(iOS 13.0, *) {

          
                // Guard statement checks if the following downcast is valid.

                // 1) If valid, returns true and the optional (if used) is unwrapped.

                // 2) If not valid, returns false exits the present function (else clause)

                guard let winScene = (AppDelegate.sForegroundScene as? UIWindowScene) else {

                    NSLog(TAG + "Error in obtaining UIWindowScene!")

                    return

                }

              
                // Create window by associating it with the scene object

                window = UIWindow(windowScene: winScene)

              
                // Initialze the ViewController and set it in the UIWindow object

                // This lets the UIWindow know which  view controller to be used to

                // display the UI.

                navcontrol = UINavigationController(rootViewController: ViewController())

                navcontrol.popToRootViewController(animated: false)

                window?.rootViewController = navcontrol


                // Displays the window. This window is set as the key window i.e displayed

                // on top of all windows and receives user-input.

                // The UI to be displayed is in ViewController.viewDidLoad

                window?.makeKeyAndVisible()

        } else {

            // TODO

        }
    }   
}



Inside the SceneDelegate, below is done :

Swift:
func scene(_ pUIScene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        NSLog(TAG + "SceneDelegate.scene(willConnectToptions)")
              
        AppDelegate.sForegroundScene = pUIScene
                
            let mywinobj = MyWindow()
          
            mywinobj.CreateWindow()
}



If the window creation code is moved inside sceneDelegate, things work smoothly.



Can you please help in what is causing this unbalanced call and how should I address this ?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.