Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

adam322

macrumors newbie
Original poster
Jul 15, 2014
4
0
Im trying to update my ui label for a consumable purchase but its not updating after the purchase. It only updates if i logout and back in.

Heres what i did:

Code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)PurchaseProduct:(id)sender{

    [[InAppPurchaseManager InAppPurchaseManagerSharedInstance] PurchaseProductWithNumber:[sender tag] Delegate:self WithSelector:@selector(Purchased:) WithErrorSelector:@selector(Error:)];
}
-(IBAction)Restore:(id)sender{

    [[InAppPurchaseManager InAppPurchaseManagerSharedInstance] Restore_ProductsWithDelegate:self WithSelector:@selector(Purchased:) WithErrorSelector:@selector(Error:)];
}
-(void)Purchased:(NSString*)product{
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"ads"];
    
    if ([product isEqualToString:Coins_500]) {
        [self AddCoins:100];
    }
    if ([product isEqualToString:Coins_1000]) {
        [self AddCoins:300];
    }
    if ([product isEqualToString:Coins_3000]) {
        [self AddCoins:1000];
    }
    if ([product isEqualToString:Coins_8000]) {
        [self AddCoins:3000];
    }
    if ([product isEqualToString:Coins_25000]) {
        [self AddCoins:10000];
    }
    if ([product isEqualToString:remove_ads]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"removeads"];
    }
    
    //updating label only.
}
-(void)AddCoins:(int)coins{

    [[DataHolder DataHolderSharedInstance].UserObject refresh];
    [DataHolder DataHolderSharedInstance].UserObject[@"coins"]=[NSNumber numberWithInt:[[DataHolder DataHolderSharedInstance].UserObject[@"coins"] intValue]+coins];
    [[DataHolder DataHolderSharedInstance].UserObject saveInBackground];

}
-(void)Error:(NSError*)error{
    
     //[[AppManager AppManagerSharedInstance ] Show_Alert_With_Title:@"Error!" message:@"Unexpected Error Occured.Please Try Again Later"];
    
}

-(IBAction)Back:(id)sender{

    [self dismissViewControllerAnimated:YES completion:nil];
}

The coin label is not updating.
 
Last edited:
Code:
-(void)AddCoins:(int)coins{

    [[DataHolder DataHolderSharedInstance].UserObject refresh];
    [DataHolder DataHolderSharedInstance].UserObject[@"coins"]=[NSNumber numberWithInt:[[DataHolder DataHolderSharedInstance].UserObject[@"coins"] intValue]+coins];
    [[DataHolder DataHolderSharedInstance].UserObject saveInBackground];

}
 
the thing is this code is for a separate ui window for a coin store but I want to update the ui label on the main window after the user closes the window. Is there anything I can call to update all ui labels for a specific window after calling the variable?
 
Here's what I tried to do:

Code:
-(void)AddCoins:(int)coins{

    [[DataHolder DataHolderSharedInstance].UserObject refresh];
    [DataHolder DataHolderSharedInstance].UserObject[@"coins"]=[NSNumber numberWithInt:[[DataHolder DataHolderSharedInstance].UserObject[@"coins"] intValue]+coins];
    [[DataHolder DataHolderSharedInstance].UserObject saveInBackground];

}
[self performSelectorOnMainThread:@selector(updateLabel)
                                           withObject:@"coins"
                                        waitUntilDone:NO];

    }

-(void)updateLabel {
    self.mycoinsLabel.text = @"coins";
}

It still doesn't work as it can't find the label but I want to change the label on another class.
 
It doesn't work because that does not make any sense. You are passing a NSString containing the word "coins" as an object. Your updateLabel method would do nothing but change the mycoinsLabel's text to "coins".

To achieve what you need, you will have to look into delegation or NSNotificationCenter, the latter being slightly easier to understand
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.