Zash__
Zash__

Reputation: 293

Present viewController from AppDelegate crashes/ empty screen

I am facing the issue that when I want to present a view programmatically my app crashes/ shows empty black view.

The opening of the vc happens when a user clicks on notifications. I have a tabbar, a navigation controller and a view which is reachable from the first vc in the tabbar.

When I want to push the vc my app crashes, however when I delete everything in the viewDidLoad() in my to be pushed to vc ( ChatVC) my app displays the vc but empty and black. My app delegate push code. Its not about the chatvc, I tried every view controller I have and it crashes at the same point.

 let chatVc = AllChatsViewController()

        if let tabbarControler = window?.rootViewController as? MainTabBarController {
            tabbarControler.selectedIndex = 0

            //tabbarControler.presentedViewController?.dismiss(animated: true, completion: nil)


            if let homeNav = tabbarControler.viewControllers?.first as? UINavigationController {
                homeNav.pushViewController(chatVc, animated: true)
                print(followerId)
            }
        }

    }

Update

  if let chatPartnerId = userInfo["chatPartnerId"] {
        let storyboard = UIStoryboard(name:"Chat", bundle: Bundle.main)
        guard let chatVC = storyboard.instantiateViewController(withIdentifier: "MessageViewController") as? MessageViewController else {return}
        chatVC.userId = chatPartnerId as! String

        if let tabbarControler = window?.rootViewController as? MainTabBarController {
            tabbarControler.selectedIndex = 0

            tabbarControler.presentedViewController?.dismiss(animated: true, completion: nil)


            if let homeNav = tabbarControler.viewControllers?.first as? UINavigationController {
                homeNav.pushViewController(chatVC, animated: true)

            }
        }

It now pushes as expected to the ViewController but neither viewDidLoad or any other function will be called, any Idea?

Upvotes: 0

Views: 584

Answers (1)

Andras M.
Andras M.

Reputation: 838

have you tried instead of let chatVc = AllChatsViewController() instantiate it from Storyboard? In storyboard find your viewController and vige it an identifier (i'll use chatVC). then let storyboard = UIStoryboard(name:"Main", bundle Bundle.main) let chatVC = storyboard.instantiateViewController(withIdentifier: "chatVC")

The reason is because if using the above method all your subviews get initiated and laid out automatically, as opposed to your method where you are required to do it yourself (which you probably haven't done hence the crash , and the fact that if you delete view did load lines that call your subviews it passes.)

Upvotes: 1

Related Questions