簡體   English   中英

如何在所有視圖控制器上設置一個標簽來設置分數?

[英]How to make one label to set score on all view controllers?

我有一個標簽,我需要它來顯示每個ViewController上的分數,硬幣等,這意味着當分數改變時,它會改變整個應用程序中的每個位置......

我試圖設置一個標簽來顯示整個應用程序的分數,但我無法弄清楚如何!

請幫忙

這就是我在視圖控制器中所擁有的:

 -(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中有一個動作(減去)-200硬幣,但它在ViewController中沒有更新?

一種方法是使用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];
}

然后在視圖控制器的某個位置添加此方法以更新UI:

- (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等。

使用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更改時對您進行通知,並且可以將此值設置為NSUserDefaults

正如大衛所說,這不是最好的方法,但是如果您需要保存數據直到應用程序關閉,您必須閱讀更多關於單身人士的信息

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM