2012-06-22 71 views
3

我想從appDelegate更新其中一個ViewController中的標籤。視圖控制器中的標籤全部在IB中鏈接並具有屬性設置。從appDelegate更新另一個視圖控制器中的標籤

在AppDelegate中.H

我創建的視圖控制器伊娃這樣我就可以在視圖控制器從這樣的委託訪問的標籤。

someViewController *myView; 

,並在AppDelegate.m文件

myView.theLabel.text = @"Whatever"; 

我也試過myView.theLabel setText: @"whatever";

香港專業教育學院還試圖使MyView的一個屬性,它合成。我試過applicationDidFinishLaunchingapplicationWillEnterForeground中的代碼,但標籤永遠不會更新。什麼是最好的方法來做到這一點?

+0

您是否在您的應用程序委託中實例化控制器,或通過任何其他方式獲取對此控制器實例的引用? – Alladinian

+0

@Alladinian在.h中提到我創建了一個ivar someViewController * myView並以這種方式訪問​​標籤。 someViewController.h頭文件被添加到appDelegate.h和appDelegate.m中,但不知道你的意思。感謝回覆。 –

+0

@Alladinian對不起我厚厚的不,我沒有從appdelegate實例化視圖控制器我應該做的標籤上面我正在訪問someViewController * myView = [[someViewController alloc] initWithNibName:@「someViewController」等等等等? –

回答

3

我會想象myView還沒有被創建,所以你有一個空引用。

試試這個來測試這一理論:

someViewController *myView; 
if(myView) 
    NSLog(@"Object has been created"); 
else 
    NSLog(@"You have a null reference - The Object hasn't been created yet"); 

如果是這樣的情況下,行更改爲這個來創建它:

someViewController *myView = [[someViewController alloc] initWithNibName:@"SomeViewController" bundle:nil]; 
myView.theLabel.text = @"Something"; 
self.window.rootViewController = myView; 

是的,你的appDelegate創建第一個觀點,然後將rootViewController設置爲第一個視圖。

相關問題