2011-08-18 114 views
1

我一直在努力幾天得到此代碼的工作,但我不知道我在做什麼錯了。每當應用程序從睡眠中醒來,或者用戶關閉應用程序並再次打開應用程序(無需關閉多任務應用程序),我想要更改標籤值。 在我的applicationDidBecomeActive中,我正在運行一個計數器,我想在任何視圖控制器當前打開時顯示該計數器。需要幫助與applicationDidBecomeActive

代碼:

- (void)applicationDidBecomeActive:(UIApplication *)application { 
counter = counter + 1; 

W1G1 *view1 = [[[W1G1 alloc] initWithNibName:@"W1G1" bundle:nil] retain]; 
[view1 setlabel]; 

}

在我的ViewController W1G1,我有以下代碼: 代碼:

- (void) setlabel { 
NSString *string = [NSString stringWithFormat:@"%d", counter]; 
vocabword.text = string; 

}

我在進口W1G1我appdelegate,但代碼不運行:(請幫助!

謝謝

+0

什麼是 「不跑」 呢?你有錯誤嗎? – jtbandes

+0

標籤不變。我通過調試器運行它,並且代碼塊正在工作,計數器和字符串根據需要進行更新,但標籤不會更改。但是,當我從我的viewController檢查計數器的值時,它顯示更新的值。但標籤根本沒有更新。 – Prajoth

+0

如果W1G1是UIViewController子類或UIView子類。如果前者,變量名稱view1是一個不尋常的選擇。您發佈的內容不清楚您的標籤如何添加到視圖層次結構中。根本沒有在視圖層次結構中更新的標籤? – Obliquely

回答

2

1)當你說'代碼不運行'你的意思是?也就是說,如果你把NSLogs放在applicationDidBecomeActive中:並且在setLabel中它是否顯示代碼正在運行?

2)我會懷疑代碼正在運行。但是你的代碼不會「在那個時候在任何視圖控制器打開時顯示計數器」。您的代碼會創建一個新視圖(視圖1),但該視圖不會顯示。它不作爲子視圖添加到任何東西。你的代碼也會泄漏。您創建了一個W1G1對象,但它永遠不會被釋放,並且會丟棄對它的任何引用。

爲了實現你想要的,你可以在應用程序的窗口中添加一個子視圖。根據您的應用程序代理是如何設置的,像下面應該做的伎倆:

counter++; 
    W1G1 *viewController1 = [[W1G1 alloc] initWithNibName:@"W1G1" bundle:nil]; 
    [viewController1 setlabel: counter]; 
    [[self window] addSubview: [viewController1 view]] 
    // you'll want to save a reference to the viewController somehow so you can release it at a later date 
在W1G1

然後

- (void) setlabel: (int) counter; 
{ 
    NSString *string = [NSString stringWithFormat:@"%d", counter]; 
    vocabword.text = string; 
} 

有,當然,很多其他的方法,你可以邁出這問題。而且您需要一些策略來移除您在某個階段添加的W1G1視圖,否則您只會添加更多視圖。

更新:你問(在評論中)如何跟蹤你的viewController整個應用程序的生命週期...一種方法是跟蹤它在你的appDelegate中。在頭部碰到這樣的:

@class W1G1; 
@interface MyAppDelegate : : NSObject <UIApplicationDelegate> 

{ 
    // other decelerations 
    int counter; 
    W1G1 * _myW1G1 
} 

@property (nonatomic, retain) W1G1* theW1G1 

在.m文件包括

@synthesize theW1G1 = _myW1G1; 

大概在應用中:didFinishLaunchingWithOptions:創建的viewController,設置屬性引用它,並添加其視圖視圖層次結構。

W1G1* theViewController = [[W1G! alloc] initWithNibName: @"W1G1" bundle: nil]; 
[[self window] addSubview: [theViewController view]]; 
[self setTheW1G1: theViewController]; 
[theViewController release]; 

然後,當你想從應用程序的委託使用[自theW1G1],例如再次訪問的viewController

[[self W1G1] setlabel: counter]; 
+0

請參閱PengOne的回答。你可以傳遞setLabel(可能是最好的)或者讓W1G1嚮應用程序代理請求計數器值。我已經修改了我的答案。 – Obliquely

+0

謝謝!當我說它不運行時,標籤不會改變。我通過調試器運行它,並且代碼塊正在工作,計數器和字符串根據需要進行更新,但標籤不會更改。但是,當我從我的viewController檢查計數器的值時,它顯示更新的值。但標籤根本沒有更新。 我嘗試了你提到的子視圖方法,並且在從不同的Objective-C類型傳遞'addSubview:'的參數1時,拋出了一個警告,指出「不兼容的Objective-C類型'結構體W1G1 *',期望'結構體UIView *' 」。 – Prajoth

+0

確定W1G1是一個viewController。你選擇的變量名稱表明這是一個觀點。修正上面的答案。但是你需要考慮你是否真的想每次都創建一個新的viewController(和視圖)(並且在某個階段之後清理),或者如果你想在應用程序的整個生命週期內跟蹤viewController。 – Obliquely

3

在AppDelegate中。M檔,那就是你有

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    counter = counter + 1; 

    W1G1 *view1 = [[[W1G1 alloc] initWithNibName:@"W1G1" bundle:nil] retain]; 
    [view1 setlabel]; 
} 

變量counter遞增僅限於AppDelegate。換句話說,你的視圖控制器不知道它已經增加了。

我建議您使用NSUserDefaults來存儲counter的值,以便您可以輕鬆地在這些視圖控制器之間傳遞它。或者,您可以允許輸入方法setLabel,例如,

- (void) setlabel:(int)counter { 
    NSString *string = [NSString stringWithFormat:@"%d", counter]; 
    vocabword.text = string; 
} 

然後在AppDelegate中,你會想做的事:

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    counter = counter + 1; 

    W1G1 *view1 = [[[W1G1 alloc] initWithNibName:@"W1G1" bundle:nil] retain]; 
    [view1 setlabel:counter]; // <-- now you're using counter 

    [self.window addSubview:view1]; 
}