2017-03-18 74 views
1

我有一個運行在tabbar類中的計時器,每次調用它時都會將數據保存到雲中。然後我希望它在當前選擇的視圖控制器上調用一個方法來告訴用戶保存了什麼。我正在做以下事情。從TabBarClass調用View Controller方法

if (self.selectedIndex == 1) { 
    MessagesViewController *msgView = [[MessagesViewController alloc]init]; 
    NSLog(@"Running"); 
    [msgView testMethod]; 

} 

這是行得通的,如果它的選擇標籤被調用的話。

NSString *teststring = [formatter stringFromDate:todaysDate]; 
NSLog(@"%@", teststring); 

self.TestLab.text = teststring; 

NSlog每次都顯示,但標籤文本沒有更新。 我相信這很簡單,但我不能拿出修復。

回答

2

MessagesViewController *msgView = [[MessagesViewController alloc]init]; 

分配一個MessagesViewController一個新的實例。它不會爲您提供對當前活動的視圖控制器實例的引用。該方法在這個新實例上執行,因此您可以得到NSLog輸出,但文本字段很可能是nil,並且至少不是在屏幕上。

您可以使用UITabBarControllerselectedViewController屬性來獲取當前選定的視圖控制器。

if (self.selectedIndex == 1) { 
    MessagesViewController *msgView = (MessagesViewController *)self.selectedViewController; 
    NSLog(@"Running"); 
    [msgView testMethod]; 
} 
+0

謝謝。正是我在找的東西。它現在讓我想到如果我需要從MessagesViewController獲取tabar類的當前實例是否有類似的調用self.selectedViewController? – joffd

+0

'self.parent'應該給你的標籤欄控制器 – Paulw11

+0

也工作過。謝謝 – joffd

-2

由於它是一個計時器,它可能在另一個線程?而且你不能在主線程之外的任何其他線程中更新UI。試試這個:

dispatch_async(dispatch_get_main_queue(), ^{ 
    self.TestLab.text = teststring; 
}); 
相關問題