WooKoo Blog

물과 같이

개발/개발

iOS - TabBar 동일 컨트롤러 사용하기

WooKoo 2020. 9. 18. 11:22

앱 델리게이트에서 다음과 같이 태그를 설정해준다.

 

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        if let tapBarController = window?.rootViewController as? UITabBarController{
            let storyboard = UIStoryboard(name: "Main", bundle: .main)
            let vc = storyboard.instantiateViewController(withIdentifier: "NavController")
            
            vc.tabBarItem = UITabBarItem(tabBarSystemItem: .topRated, tag: 1)
            tapBarController.viewControllers?.append(vc)
        }

 

하고 뷰 컨트롤러에서는 이런식으로 tabBarItem Tag에 따라 다른 방식을 적용해준다.

        let urlString:String
        if navigationController?.tabBarItem.tag == 0{
            urlString = "https://www.hackingwithswift.com/samples/petitions-1.json"
        }else{
            urlString = "https://www.hackingwithswift.com/samples/petitions-2.json"
        }