如何从viewControllerA发送值到viewControllerB?

时间:2014-04-24 11:58:10

标签: ios objective-c

UIButton上标题为---> Free Coins - Press <----的{​​{1}}已被按下。

viewControllerA

viewControllerA
点击后获得50个硬币:

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

但这些硬币只能出现在名为// 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]]; } 的标签viewControllerB上。或者至少将两者显示为相同的值!

我试过使用整数:例如lblptsint point,例如:

objectForKey:@"point"

但似乎,我无法仅在savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; nPoint = [[savedStock objectForKey:@"point"] intValue]; [_lblpts setText:[NSString stringWithFormat:@"%d",nPoint]]; 标签上显示余额!

4 个答案:

答案 0 :(得分:1)

你需要在viewControllerB中有一个属性变量,同时推动viewControllerB用点初始化该变量。

如果您正在使用故事板,那么:

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

prepareForSegue方法中,使用最新点更新属性变量。

-(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.
    }
}

如果您正在使用XIB,那么您可以在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];

ViewControllerB.h文件中,将属性声明添加为:

@property (nonatomic, assign) NSInteger points;

ViewControllerB.m档案中:

- (void)viewDidLoad
{
    NSString *str = [NSString stringWithFormat: @"%d", self.points];
    [self.pointsLbl setText:str];
}

答案 1 :(得分:0)

您可以使用自定义委托方法或通知中心方法在两个视图控制器之间传递数据。使用属性也是传递数据的好方法,但是如果你有很多信息从一个视图控制器传递到另一个视图控制器那么你应该去自定义委托方法。

答案 2 :(得分:0)

这似乎是从viewControllerA传递到viewControllerB的转发消息。这可以通过以下简单方法轻松实现:

我们假设您在viewControllerB中有一个方法:

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

现在在你的viewControllerA中,你只需要这样做:

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

这就是全部。使用ViewControllerB的点属性获取积分。

答案 3 :(得分:0)

您可以使用NSNotificationCenter来广播硬币更新事件。所以你的代码似乎是:

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

请注意,此处delegateself。现在实现delegate,如:

- (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];
}

并在viewControllerB中听取此通知:

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

功能实现如下:

- (void)updateCurrencyBalance:(NSNotification*)_notifObject
{
    NSNumber *wrappedBalance = [_notifObject object];
    NSUInteger balance = wrappedBalance && [wrappedBalance isKindOfClass:
        [NSNumber class]] ? [wrappedBalance unsignedIntValue] : 0;
    [currencyLabel setText:[NSString stringWithFormat:@"%u", balance]];
}