在 swift3 中从 AppDelegate 调用函数

佩拉约·马丁内斯

我正在使用OneSignal我的应用程序处理通知。这是在AppDelegate.swift有处理接收到的应用程序的函数的地方启动的

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

然后我有TabViewController5 个选项卡,每个选项卡的导航项都有一个UINavigationBarButton将您发送到消息视图。每当收到通知以显示用户收到新消息时,我都想更改该按钮中的徽章编号。我在每个 viewController(5 个选项卡中的每一个)中都有一个函数来更新它。

问题是AppDelegate需要调用这个函数updateBadgeNumber,我不知道如何做到这一点。另外,某些选项卡甚至可能尚未初始化。有谁知道如何从AppDelegate?

谢谢你。

编辑:

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

保尔维斯

答案取决于您是否UITabBarController是应用程序的根视图控制器。

  1. 如果您UITabBarController是根视图控制器,您可能在应用程序委托中引用了它,并且可以使用UITabBarControllerselectedViewController属性来获取当前视图控制器,然后设置徽章值。(如果您没有引用,您可以强制window.rootViewController转换UITabBarController为访问它。)
  2. 如果不是的话,你可以使用代码这样找到当前可见视图控制器。(然后要访问UITabBarController,您可以强制转换parentViewController为 a UITabBarController,然后从那里访问其他视图控制器并设置它们的标记值。)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Appdelegate.swift中的调用函数

Firebase Deeplink在Swift 3中未调用AppDelegate的application:continueUserActivity:restorationHandler函数

如何从AppDelegate中的ViewController调用函数?

在AppDelegate中未调用的委托函数

AppDelegate中的managedObjectContext函数

如何使用Xamarin形式在PCL类中调用Appdelegate函数?

未在appDelegate中调用remoteControlReceivedWithEvent

在Swift3中,合并响应并调用一次?

AppDelegate中的Swift UITabbar阴影

从AppDelegate调用的方法中应有预期的“]”

如何在appdelegate中调用viewController的方法

Swift中AppDelegate中的网络通话

从 AppDelegate 调用 GameScene 方法(Swift 3、SpriteKit、Xcode 8)

尝试在appdelegate中调用AdColony.configure时,Swift / Xcode AdColony返回错误

如何在 AppDelegate.swift 中按常规顺序调用 ViewController

Swift3中的NSCalendar

Xcode; 如何从另一个ViewController调用AppDelegate中的函数

从appDelegate中的SQL文件进行Swift 3预加载

我不知道为什么swift3中没有调用viewForHeaderInSection

使用Swift3中的完成处理程序调用Objective C方法(完成是IUO吗?)

当我在 swift3 中调用 becomeFirstResponder 时,屏幕正在向上移动

如何引用AppDelegate.swift中的方法?

在Swift中从AppDelegate获取ViewController的实例

Swift:如何访问AppDelegate中的navigationController

Swift 中的函数调用问题

调用函数时,函数中的项目变为 nil - Swift 3

void函数Swift3中意外的非无效返回值

swift3 append 函数在 firebase observeSingleEvent 中不起作用

如何将文本字段的值传递给 Swift3 中的按钮单击函数?