0

A UIButton with title ---> Free Coins - Press <---- on viewControllerA is pressed.

viewControllerA:

- (IBAction)triggerVideo
{
    [AdColony playVideoAdForZone:@"HIDDEN-CODE-PRIVACY" withDelegate:nil
        withV4VCPrePopup:YES andV4VCPostPopup:YES];
}

50 coins are gained when it's clicked:

// 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;
    [currencyLabel setText:[NSString stringWithFormat:@"%u", balance]];
}

But these coins must only show up on viewControllerB, on the label called lblpts. Or at least show up on both as the same values!

I've tried to use ints: e.g. int point, and objectForKey:@"point", e.g.:

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

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

But it seems, I can't make the balance appear only on the viewControllerB label!

Lebyrt
  • 1,376
  • 1
  • 9
  • 18
Jking
  • 35
  • 7

4 Answers4

1

You need to have a property variable in viewControllerB and while pushing viewControllerB initialize that variable with points.

If you are using storyboard then:

[self performSegueWithIdentifier:@"SegueB" sender:self];

In prepareForSegue method update the property variable with latest points.

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"SegueB"]) {
        DetailVCB *detailB = (DetailVCB *)segue.destinationViewController;
        detailB.points = 50; // the variable which has latest points.
    }
}

If you are using XIB then you can achieve it adding this in ViewControllerA.m:

ViewB *ScreenB = [[[ViewB alloc]initWithNibName:@"ViewBNib" bundle:nil]
    autorelease];
ScreenB.points = balance; //balance is the variable having latest points
[self.navigationController pushViewController:ScreenB animated:YES];

In ViewControllerB.h file add the property declaration as:

@property (nonatomic, assign) NSInteger points;

In ViewControllerB.m file:

- (void)viewDidLoad
{
    NSString *str = [NSString stringWithFormat: @"%d", self.points];
    [self.pointsLbl setText:str];
}
Lebyrt
  • 1,376
  • 1
  • 9
  • 18
user2071152
  • 1,161
  • 1
  • 11
  • 25
0

You could use a custom delegate method or notification center method for passing data between two view controller's. Also using property is also good way for passing data but if you got a lot of info to pass from one view controller to another then you should go for custom delegate method.

nikhil84
  • 3,235
  • 4
  • 22
  • 43
0

This seemed like a forward message passing from viewControllerA to viewControllerB. This can be easily achieved with a simple method below:

Let's say you have a method in viewControllerB:

- (void)UpdatepPoints:(int)pts {
    _point = pts;
}

Now in your viewControllerA, you just have to do something like this:

ViewControllerB *vc = [ViewControllerB alloc] init];
[vc UpdatepPoints:pointshere];

That's all. Use point property of ViewControllerB for getting points.

Lebyrt
  • 1,376
  • 1
  • 9
  • 18
  • but while pushing the ViewControllerB again one more instance of ViewControllerB will be created in that instance the points variable will not be updated. Hence can't do just by allocating an instance of ViewControllerB. – user2071152 Apr 24 '14 at 12:23
  • Should I make two labels and pass points from one the label in VC-A to the Label in VC-B. Or can I just Make it appear in VC-B with the above code. – Jking Apr 24 '14 at 12:23
0

You can use NSNotificationCenter to broadcast coin update event. So your code would seems like:

- (IBAction)triggerVideo
{
    [AdColony playVideoAdForZone:@"HIDDEN-CODE-PRIVACY" withDelegate:self
        withV4VCPrePopup:YES andV4VCPostPopup:YES];
}

Note here delegate is self. Now implement delegate like:

- (void)onAdColonyV4VCReward:(BOOL)success currencyName:(NSString*)currencyName
    currencyAmount:(int)amount inZone:(NSString*)zoneID
{       
    NSNumber *number = [[NSNumber alloc] initWithInt:amount];
    [[NSNotificationCenter defaultCenter] postNotificationName:
        @"kCoinUpdateNotification" object:number];
}

And listen for this notification in viewControllerB like this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(
    updateCurrencyBalance:) name:@"kCoinUpdateNotification" object:nil];

And function implementation goes like:

- (void)updateCurrencyBalance:(NSNotification*)_notifObject
{
    NSNumber *wrappedBalance = [_notifObject object];
    NSUInteger balance = wrappedBalance && [wrappedBalance isKindOfClass:
        [NSNumber class]] ? [wrappedBalance unsignedIntValue] : 0;
    [currencyLabel setText:[NSString stringWithFormat:@"%u", balance]];
}
Lebyrt
  • 1,376
  • 1
  • 9
  • 18
Raman soni
  • 86
  • 4