gpt4 book ai didi

ios - 在 swift3 中从 AppDelegate 调用函数

转载 作者:可可西里 更新时间:2023-11-01 02:05:05 25 4
gpt4 key购买 nike

我正在使用 OneSignal 处理我应用中的通知。这是在 AppDelegate.swift 中启动的,其中有一个处理接收到的应用程序的函数:

let notificationReceivedBlock: OSHandleNotificationReceivedBlock = { notification in
print("Received Notification: \(notification!.payload.body)")
}

然后我有一个带有 5 个选项卡的 TabViewController,每个选项卡的 navigationItem 都有一个 UINavigationBarButton,它将您发送到消息 View 。每当有通知显示用户收到新消息时,我想更改该按钮中的角标(Badge)编号。我在每个 viewController(5 个选项卡中的每一个)中都有一个函数来更新它。

问题是 AppDelegate 需要调用此函数 updateBadgeNumber 而我不知道该怎么做。另外,一些选项卡甚至可能还没有初始化。有谁知道如何从 AppDelegate 调用 ViewControllers 中的函数?

谢谢。

编辑:

感谢下面的@paulvs 找到了解决方案。他将我链接到我发现这个的答案:Find Top View Controller in Swift

最佳答案

答案取决于您的 UITabBarController 是否是您应用的 Root View Controller 。

  1. 如果您的 UITabBarController 是 Root View Controller ,您可能在您的应用委托(delegate)中引用了它并且可以使用 UITabBarControllerselectedViewController 属性获取当前 View Controller ,然后设置角标(Badge)值。 (如果您没有引用,您可以将 window.rootViewController 转换为 UITabBarController 来访问它。)
  2. 如果不是,您可以使用代码 like this找到当前可见的 View Controller 。 (然后要访问 UITabBarController,您可以将 parentViewController 转换为 UITabBarController,然后从那里访问其他 View Controller 并设置它们的标记值,也是。)

关于ios - 在 swift3 中从 AppDelegate 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43552230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com