2014-04-23 65 views
1

我有一個標籤,我需要它來顯示比分,在每個視圖控制器硬幣ECT,這意味着,當比分改變它在那裏在整個應用程序的每個變化......如何使一個標籤在所有視圖控制器上設置分數?

我試圖設置標籤以顯示整個應用程序的分數,但我不知道如何!

請幫

這是我迄今爲止在視圖控制器:

-(void)viewDidLoad 
{ 
{ 
[super viewDidLoad]; 


    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1 
    NSString *documentsDirectory = [paths objectAtIndex:0]; //2 
    path = [documentsDirectory stringByAppendingPathComponent:@"SettingsList.plist"]; //3 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    if (![fileManager fileExistsAtPath: path]) //4 
    { 
     NSString *bundle = [[NSBundle mainBundle] pathForResource:@"SettingsList"ofType:@"plist"]; //5 //5 

     [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6 
    } 

    savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 

    nPoint = [[savedStock objectForKey:@"point"] intValue]; 
    [giftAmount setText:[NSString stringWithFormat:@"%d",nPoint]]; 

[self updateCurrencyBalance]; 
[self zoneLoading]; 
} 

//adcolony 
- (void) viewDidAppear:(BOOL)animated 
{ 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateCurrencyBalance) name:kCurrencyBalanceChange object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(zoneReady) name:kZoneReady object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(zoneOff) name:kZoneOff object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(zoneLoading) name:kZoneLoading object:nil]; 
} 

// Get currency balance from persistent storage and display it 
- (void)updateCurrencyBalance { 
NSNumber* wrappedBalance = [[NSUserDefaults standardUserDefaults] objectForKey:kCurrencyBalance]; 
NSUInteger balance = wrappedBalance && [wrappedBalance isKindOfClass:[NSNumber class]] ? [wrappedBalance unsignedIntValue] : 0; 
[giftAmount setText:[NSString stringWithFormat:@"%u", balance]]; 

[savedStock setObject:[NSNumber numberWithFloat:nPoint = balance] forKey:@"point"]; 
[savedStock writeToFile: path atomically:YES]; 

} 

我在其他PlayViewController這(minusus)動作-200硬幣,但它沒有更新中ViewController?

+0

您的意思是您爲每個viewController添加了標籤? – EridB

+0

關注價值觀察。 – Nick

+0

@EridBardhaj是的,但每個標籤必須具有相同的信息/數據! – Jking

回答

2

一種方法是使用NSNotificationCenter。

這個代碼添加到改變你的分數的值的所有地方:

- (void)updateScore:(NSNumber *)newValue 
    // update the score 
    self.score = newValue; 

    // create an dictionary object containing the score to be sent with the notification 
    NSMutableDictionary* userInfo = [NSMutableDictionary dictionary]; 
    [userInfo setObject:self.score forKey:@"score"]; 

    // Add this to send a notification to all the listeners in the whole app 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationScoreChanged" object:nil userInfo:userInfo]; 
} 

在您的視圖控制器的viewDidLoad方法,添加以下代碼:在某處

- (void)viewDidLoad:(BOOL)animated 
{ 
    [super viewDidLoad:animated]; 

    // Add this code to start receiving notifications (become a listener) 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scoreChanged:) name:@"NotificationScoreChanged" object:nil]; 
} 

然後你視圖控制器,添加此方法來更新您的用戶界面:

- (void)scoreChanged:(NSNotification *)notification 
{ 
    // retrieve the score results 
    if ([notification.name isEqualToString:@"NotificationScoreChanged"]) 
    { 
     NSDictionary *userInfo = notification.userInfo; 
     NSNumber *score = [userInfo objectForKey:@"score"]; 

     // and update your labels 
     self.scoreLabel.text = [score description]; 
    } 

而在你的視圖控制器,添加的dealloc:

- (void)dealloc 
{ 
    //Unregister yourself (stop listening) 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

注意:你應該取決於你如何存儲和檢索你的分數適應的代碼。也就是說,如果您使用NSUserDefaults(請參閱@ erid的答案),CoreDate等。

1

使用NSUserDefaults存儲數據(只有當程序從iOS設備中刪除或者您可以手動刪除時纔會刪除數據)。在NSUserDefaults的

儲值

//text is your label.text, and each time you change it, save it to user details 
NSString *text; 

//Store them to NSUserDefaults with a specific key 
[[NSUserDefaults standardUserDefaults] setObject:text forKey:@"label"]; 

獲得值回

NSString *textValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"label" ]; 
label.text = textValue; 

你可以嘗試添加NSNotification到你的程序時NSString設置更改標籤更改notificate你,有你可以將此值設置爲NSUserDefaults

正如大衛說,這不是要做到這一點的最好辦法,但你要了解更多關於單身,如果你需要保存DATA,直到應用程序被關閉。

希望它有幫助

+0

對於像遊戲中的分數那樣短暫的事情,使用NSUserDefaults是相當適得其反的,這並不能真正回答OP如何協調變化的問題。 –

+0

我很抱歉,但是當您設置標籤時,您也可以在NSUserDefaults上設置值。無論如何,如果我編輯與@Van du Tran問題合併的問題,這實現了用戶需要的目標,以便與您保持一致? – EridB

+0

使用NSUserDefaults來存儲經常變化的內容(推斷我們在這裏討論的是什麼樣的'分數')通常是不好的建議,因爲同步和一般訪問費用很高。從NSUserDefaults參考「NSUserDefaults類提供了一個用於與默認系統進行交互的編程接口。默認系統允許應用程序根據用戶的喜好自定義其行爲。」 –

相關問題