IAP完成后隐藏/取消隐藏IBOutlet(UIButton)

时间:2013-08-14 17:47:33

标签: iphone ios objective-c

好吧,所以我现在已经玩了好几天了,我还没有让它工作,期待你的帮助! 基本上我想在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");
}

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

1 个答案:

答案 0 :(得分:2)

您可以使用NSNotificationCenter

在InputViewController.m的viewDidLoad:方法中添加以下代码行:

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

在IAPStore.m的purchaseComplete方法中,替换为:

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

用这个:

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

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

此外,您还需要将此方法添加到InputViewController.m中,以便在取消分配时将其作为观察者删除。

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"purchaseCompleteNotification" object:nil];
}