NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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.