2

好的,所以我已经玩了几天了,我还没有得到这个工作,期待你的帮助!基本上,一旦 IAP 完成,我想“解锁”一项功能。我有 IAP 代码可以工作,但我想更改按钮“sendMail”(在 Interface Builder 中为“禁用”),以便用户可以与之交互。

//InputViewController.h
#import "IAPStore.h"
@interface InputViewController : UIViewController <MFMailComposeViewControllerDelegate, UIAlertViewDelegate>
@property(strong,nonatomic)IBOutlet UIButton *sendMail;
-(void)enableMail;
....
@end

//InputViewController.m
#import "InputViewController.h"
#import "IAPStore.h"
-(void)enableMail
{
  [_sendMail setEnabled:YES];
  NSLog(@"Unlocking Button");
}

//IAPStore.h
#import "InputViewController.h"
@interface IAPHelper : NSObject <UIAlertViewDelegate>
-(void)purchaseComplete;
...
@end

//IAPStore.m
#import "InputViewController.h"
-(void)purchaseComplete
{
   UIAlertView *purchased = [[UIAlertView alloc]initWithTitle:@"In-App Purchase"   message:@"Purchase complete! Thank you!" delegate:nil
   cancelButtonTitle:@"OK" otherButtonTitles:nil];
   GROWInputViewController *viewController = [[GROWInputViewController alloc] init];
   [viewController enableMail];
   [purchased show];
   NSLog(@"button enabled");
}

所以它也打印出日志,但另一个视图控制器上没有任何改变,但没有任何改变,知道我做错了什么吗?

4

1 回答 1

3

你可以使用NSNotificationCenter

viewDidLoad:InputViewController.m 的方法中添加这行代码:

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

purchaseCompleteIAPStore.m 的方法中,替换为:

GROWInputViewController *viewController = [[GROWInputViewController alloc] init];
[viewController enableMail];

有了这个:

[[NSNotificationCenter defaultCenter] postNotificationName:@"purchaseCompleteNotification" object:nil];

这将导致在购买完成时发布通知。同时,InputViewController有一个“观察者”设置为在发布该通知时调用您的“启用邮件”方法。

此外,您需要将此方法添加到 InputViewController.m,以便在释放时将其作为观察者移除。

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"purchaseCompleteNotification" object:nil];
}
于 2013-08-14T18:06:16.527 回答