我有一个使用应用内购买的应用。 单击主视图控制器中的按钮可加载第二个视图控制器,其中包含恢复购买,购买产品和取消。有一个委托协议来解除视图。 当我clic取消它工作正常,但如果我以任何商店观察员方法编程设置dismiss它不起作用。通过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;
}
ViewController.h
#import <UIKit/UIKit.h>
#import "storeNavigationController.h"
@class completeView;
@interface ViewController : UIViewController <StoreNavigationControllerDelegate>
- (void)storeNavigationControllerDidFinish;
@end
ViewController.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
}
答案 0 :(得分:0)
代表应该使用assign属性。
@property (assign, nonatomic) id <StoreNavigationControllerDelegate> delegate;
但这可能不是你的问题。您是否尝试在完成事务或恢复事务之前调用委托方法。您是否设置了断点或使用NSLog在调用这些方法之前检查委托是否为nil?
NSLog(@"delegate: %@", self.delegate);