【问题标题】:How to update the value in a VC when a tab is selected from a UITabbarController?从 UITabbarController 中选择选项卡时如何更新 VC 中的值?
【发布时间】:2020-05-04 22:12:20
【问题描述】:

我有一个标签栏控制器。当用户单击选项卡栏按钮之一时,我需要更新目标视图控制器中的 UIPageViewController 中的值。

我正在尝试使用委托通知 UIPageViewController 点击了哪个标签栏按钮:

protocol PlanTypeDelegate {
    func setIntro(thisFlow planType: UITabBarItem)
}

class NewTabBarController: UITabBarController {

var planTypeDelegate : PlanTypeDelegate?

override func viewDidLoad() {
        super.viewDidLoad()

        // create and handle tab bar button actions
}


override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        planTypeDelegate?.setIntro(thisFlow: item)
    }

在我的 UIPageController 中,我有以下内容:

class IntroPageController: UIPageViewController {

override func viewDidLoad() {
        super.viewDidLoad()

guard let tabbar = self.parent as? NewTabBarController() else { return }
    tabbar.delegate = self

}

}

extension IntroPageController : PlanTypeDelegate {
    func setIntro(thisFlow planType: UITabBarItem) {
        print("this item:\(planType)")
    }
}

相反,我收到以下错误消息:

我不熟悉在 VC 之间传递数据,所以我不知道如何处理这种情况。

编辑 更新后同样的错误

【问题讨论】:

    标签: ios swift delegates protocols


    【解决方案1】:

    你可以像这样实现它.. 没有委托......在IntroPageController 中写setIntro 方法我希望它能解决你的问题

    class NewTabBarController: UITabBarController {
    override func viewDidLoad() {
            super.viewDidLoad()
            self.delegate = self
    
        }
    }
     func tabBarController(_ tabBarController: UITabBarController,
                              shouldSelect viewController: UIViewController) -> Bool{
    
            if let controller = viewController as? IntroPageController {
    
                controller.setIntro(thisFlow: tabBarController.tabBarItem)
            }
            return true
        }
    

    你也可以通过Protocol for that write来实现...所有确认PlanTypeDelegate的控制器都可以对该方法执行操作

     func tabBarController(_ tabBarController: UITabBarController,
                              shouldSelect viewController: UIViewController) -> Bool{
    
            if let navController = viewController as? UINavigationController {
                if let myViewController  = navController.topViewController , let homeController = myViewController as? PlanTypeDelegate {
                    homeController.setIntro(thisFlow: tabBarController.tabBarItem)
                }
            } else if let controller = viewController as? PlanTypeDelegate {
    
                controller.setIntro(thisFlow: tabBarController.tabBarItem)
            }
            return true
        }
    

    【讨论】:

    • 谢谢,但是第一种方法不起作用。对于第二种方法,如何在 IntroPageController 类中设置委托?
    • 应该是tabbar.planTypeDelegate = self 吗?
    • 好吧,第一个应该可以工作...请确认 IntroPageController 有 navigationController 吗?
    • 不,不是因为它是 UIPageViewController 的子类。
    • 检查第二种方法......你不需要写委托......测试它......它肯定会工作
    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多