UI label not updating after consumable purchase

Discussion in 'iOS Programming' started by adam322, Jul 15, 2014.

  1. adam322, Jul 15, 2014
    Last edited: Jul 15, 2014

    adam322 macrumors newbie

    Joined:
    Jul 15, 2014
    #1
    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.
     
  2. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
  3. adam322 thread starter macrumors newbie

    Joined:
    Jul 15, 2014
    #3
    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];
    
    }
    
     
  4. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #4
    That code updates a variable, not an UILabel's text
     
  5. adam322 thread starter macrumors newbie

    Joined:
    Jul 15, 2014
    #5
    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?
     
  6. adam322 thread starter macrumors newbie

    Joined:
    Jul 15, 2014
    #6
    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.
     
  7. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #7
    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
     

Share This Page