ios - Swift:从不同的选项卡中调用函数

标签 ios swift uitabbarcontroller

我要调用函数

 func setupChatLogForUser(user: ChatPartner){
    let layout = UICollectionViewFlowLayout()
    let chatLog = ChatHistory(collectionViewLayout: layout)
    chatLog.user = user
    navigationController?.pushViewController(chatLog, animated: true)
}

在我的 TabBarController 的不同选项卡中。

作为第一步,我更改了选项卡,但之后不知道如何调用特定用户的函数,因为我也使用 navigationController 来推送 View Controller ...

感谢您的帮助!

编辑:

var chatsViewController:HomeScreen?

func chat(){
    print("Chat clicked")
    guard let chatFriends = self.friend else {return}
    print(chatFriends)

    self.tabBarController?.selectedIndex = 0
    self.chatsViewController?.setupChatLogForUser(user: chatFriends)
}

最佳答案

您没有在此处初始化 chatsViewControllerHomeScreen 实例,根据您发布的代码看起来它是 nil。您可以像这样调用方法而无需全局创建实例

func chat(){
    print("Chat clicked")
    guard let chatFriends = self.friend else {return}
    print(chatFriends)

    self.tabBarController?.selectedIndex = 0
    HomeScreen().setupChatLogForUser(user: chatFriends)
}

希望对您有所帮助。

关于ios - Swift:从不同的选项卡中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45895201/

相关文章:

ios - 如何在Swift中构建React Native包?

java - RoboVM 绑定(bind)编译问题

c++ - 在 Xcode 中诊断来自 Swift 的 iOS C++/Objective C++ 调用

ios - 使用 swift 和 Xcode 6.3 在后台连续下载多个文件

swift - UITabBarController.selectedIndex 崩溃

iOS:如何选择 UITabBar 中的特定选项卡和该选项卡中的特定行

iOS 6.1 与 willTransitionToState 的行为不同

ios - 将图像添加到粒子发射器并在 swift/ios 中持续一段时间后停止

swift - 无法将字符串转换为 NSDate

ios - 以编程方式在 loginVC 之后嵌入自定义选项卡栏 Controller