ktm125
ktm125

Reputation: 452

Swift: Call Function from within a different tab

I want to call the function

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

from within a different tab of my TabBarController.

I´m changing the tab as the first step but don´t know how to call the function with a specific user afterwards as I´m using the navigationController to push a view controller as well...

Thank´s for your help!

EDIT:

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)
}

Upvotes: 0

Views: 1279

Answers (1)

Aditya Srivastava
Aditya Srivastava

Reputation: 2650

You are not initialising the chatsViewController an instance of HomeScreen here and looks like it's nil as per your code posted. You can call the method like this without globally creating an instance

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

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

Hope it helps.

Upvotes: 1

Related Questions