WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

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) {
}

4      

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.

1      

@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!

1      

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.

1      

@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 }

1      

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.