tabBar 메소드로는 원하는 것이 구현이 되는데 똑같이 선택한 tabBar item을 알려주는 tabBarController로 구현하면 안되는것일까? 이 두 함수의 차이점을 알아보도록 하자
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
Sent to the delegate when the user selects a tab bar item.
사용자가 탭 표시줄 항목을 선택하면 delegate에게 전송됩니다.
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
Tells the delegate that the user selected an item in the tab bar.
사용자가 탭 표시줄에서 항목을 선택했음을 delegate에게 알립니다.
extension ViewController: UITabBarControllerDelegate {
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if(item.title == "Second") {
print(item.title)
index = 1
} else {
index = 0
}
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if index != 1 {
return true
}
return false
}
}
extension ViewController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if index != 1 {
return true
}
return false
}
// delegate에게 사용자가 탭 표시줄에서 항목을 선택했다고 알려준다.
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if(tabBarController.selectedIndex == 2) {
index = 1
} else {
index = 0
}
print(tabBarController.selectedIndex)
}
}
왜 tabBarController를 사용한 두 번째 코드는 2번째 item을 누르고 나면 tabBar 자체가 비활성화 될까?🤔
https://developer.apple.com/documentation/uikit/uitabbardelegate/1623463-tabbar#parameters
https://developer.apple.com/documentation/uikit/uitabbarcontrollerdelegate/1621173-tabbarcontroller