UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Hacking with Swift: Project 7 - AppDelegate

Forums > Books

In Hacking with Swift, Project 7, I discovered an error which took me a while to understand and come up with a fix, but here it is.

In the tutorial you say to add the following to AppDelegate…

 if let tabBarController = window?.rootViewController as? UITabBarController {

      let storyboard = UIStoryboard(name: "Main", bundle: nil)
      let vc = storyboard.instantiateViewController(withIdentifier: "NavController")
      vc.tabBarItem = UITabBarItem(tabBarSystemItem: .topRated, tag: 1)

      tabBarController.viewControllers?.append(vc)
    }

I believe that this code must be placed in SceneDelegate.swift.

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

6      

Yeah, just hit the same thing. var window: UIWindow? is no longer declared in AppDelegate, but SceneDelegate. i agree with the suggested method, adding the code there works as expected.

3      

@ThomasLeeEvans - Thank you so much for posting this! I was wondering why my second tab bar item was not showing up. This solved my issue!

@tarasis - also noticed that var window: UIWindow? wasn't in AppDelegate.swift so I declared it my elf but ran into the problem of the code having no effect of adding the second button.

Much appreciated!

3      

I did what you did but the other tabBarController doesnt show up, i already checked the code and it's everything ok! does anyone has the same problem? thanks.

3      

@ThomasLeeEvans is correct.

Place the code in SceneDelegate.swift in the func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) after guard let _ = (scene as? UIWindowScene) else { return }

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.