0

我有一个使用应用内购买的应用程序。单击主视图控制器中的按钮会加载第二个视图控制器,其中包含恢复购买、购买产品和取消。有一个委托协议来关闭视图。当我单击取消时它工作正常,但如果我在任何商店观察者方法中以编程方式设置关闭它不起作用。通过 NSNotification 它似乎可以工作,但我想知道它为什么不工作。尝试从那里更改按钮的 alpha 值也不起作用。先感谢您。

Appdelegate.h

#import <UIKit/UIKit.h>
#import "StoreNavigationController.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic)  StoreNavigationController* observer;
@end

Appdelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize observer;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    observer = [[StoreNavigationController alloc] init];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:observer];
    return YES;
}

视图控制器.h

#import <UIKit/UIKit.h>
#import "storeNavigationController.h"

@class completeView;
@interface ViewController : UIViewController <StoreNavigationControllerDelegate>
- (void)storeNavigationControllerDidFinish;
@end

视图控制器.m

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
 if ([[segue identifier] isEqualToString:@"storeView"]) {
 [[segue destinationViewController] setDelegate:self];

     }
 }

- (void)storeNavigationControllerDidFinish
{
    [self updateView];
    [self dismissModalViewControllerAnimated:YES];
}
//There is also a method in this file launching [self performSegueWithIdentifier:@"storeView" sender:self];

storeNavigationController.h

#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
#import "plistController.h"

@protocol StoreNavigationControllerDelegate <NSObject>
- (void)storeNavigationControllerDidFinish; //View dismisser
@end

@class StoreNavigationController;
@interface StoreNavigationController : UIViewController <SKPaymentTransactionObserver>
@property (strong, nonatomic) id <StoreNavigationControllerDelegate> delegate;
//User actions
- (IBAction)restoreItems:(UIButton *)sender;
- (IBAction)buyItem:(UIButton *)sender;
- (IBAction)cancelPurchase:(UIButton *)sender;

//Methods SKPaymentTransactionObserver 
- (void) completeTransaction:(SKPaymentTransaction *)transaction;
- (void) failedTransaction:  (SKPaymentTransaction *)transaction;
- (void) restoreTransaction: (SKPaymentTransaction *)transaction;
- (void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions;
@end

storeNavigationController.m

- (IBAction)cancelPurchase:(UIButton *)sender {
[self.delegate storeNavigationControllerDidFinish]; //this one works ok
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                [self.delegate storeNavigationControllerDidFinish]; //Any of those calls in the switch doesn't work.
                break;
             case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                [self.delegate storeNavigationControllerDidFinish];
                break;
             case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
                [self.delegate storeNavigationControllerDidFinish];
             default:
                break;
         }
     }
  }

 - (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray *)transactions       {
    for (SKPaymentTransaction* transaction in transactions) {
         NSLog(@"Transacción eliminada de la cola: %@", transaction.description);
     }
           [self.delegate storeNavigationControllerDidFinish]; //Not working

  }
4

1 回答 1

0

代表应该使用 assign 属性。

 @property (assign, nonatomic) id <StoreNavigationControllerDelegate> delegate;

但这可能不是你的问题。您是否尝试在完成事务或恢复事务之前调用委托方法。在调用这些方法之前,您是否设置了断点或使用 NSLog 检查委托是否不为零?

NSLog(@"delegate: %@", self.delegate);
于 2012-08-14T16:41:02.930 回答