2017-08-26 48 views
0

內呼叫功能我想從我的TabBarController的不同選項卡內調用函數斯威夫特:從不同的標籤

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

I'm改變標籤的第一步,但鴕鳥政策知道如何使用navigationController推視圖控制器以及事後打電話與特定用戶的功能I'm ...

謝謝你的幫助!

編輯:

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

從其他類中獲取該TabBar類的實例並調用該方法。 – TheValyreanGroup

+0

試過了,沒有工作。當在同一個選項卡中調用該函數時,此工作正常。但是,從不同的標籤內呼叫時,它似乎無法正常工作。 – ktm125

+0

請顯示您試圖稱呼它的內容。 – TheValyreanGroup

回答

1

你沒有初始化的chatsViewControllerHomeScreen這裏的實例和看起來像它的無按你的代碼發佈。你可以在沒有全局創建實例的情況下調用這樣的方法

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

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

希望它有幫助。

+0

感謝您的回答!它不工作,雖然... – ktm125

+0

調試它,並檢查setupchatlogforuser方法是否被調用 –

+0

是的,它確實被稱爲 – ktm125