按下按钮时UIViewcontroller无法打开

时间:2014-01-09 03:33:07

标签: ios objective-c xcode uiviewcontroller uibutton

我在运行和测试应用时出现问题。

我一直在使用GeekyLemon提供的教程。

第1部分:http://www.geekylemon.com/xcode-in-app-purchase-part-1

第2部分:http://www.geekylemon.com/xcode-in-app-purchase-part-2

第3部分:http://www.geekylemon.com/xcode-in-app-purchase-part-3

第4部分:http://www.geekylemon.com/xcode-in-app-purchase-part-4

本教程教您如何在应用中使用应用内购买,并使用它为您的应用创建“移除广告”功能。

完成本教程的第二部分后,我决定运行我的代码,看看到目前为止我已经完成了什么。当我运行我的应用程序时,带有“删除广告”按钮的视图控制器会完美显示。但是,当我点击按钮时,它会将我发送到一个黑色的屏幕,而不是我希望它去的视图控制器。

这是我的storyboard文件中我的两个视图控制器的屏幕截图。左侧的视图控制器有我的“删除广告”按钮。单击它时,我希望它转到右侧的视图控制器。

enter image description here

以下是触摸“移除广告”按钮之前和之后我的应用在模拟器中的屏幕截图:

enter image description here

enter image description here

我的SettingsViewController.h(故事板中我的第一个View控制器的自定义类):

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <StoreKit/StoreKit.h>
#import "PurchasedViewController.h"

@interface SettingsViewController : UITableViewController <   MFMailComposeViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;

@property (weak, nonatomic) IBOutlet UILabel *Label;
- (IBAction)PurchaseItem:(id)sender;
@property (strong, nonatomic) PurchasedViewController *purchaseController;
-(void)Purchased;

@end

我的设置ViewController.m:

#import "SettingsViewController.h"
#import "SWRevealViewController.h"
#define k_Save @"Saveitem"


@interface SettingsViewController ()

@property (nonatomic, strong) NSArray *menuItems;

@end

@implementation SettingsViewController

@synthesize Label;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
} 

- (void)viewDidLoad
{
[super viewDidLoad];
_sidebarButton.target = self.revealViewController;
_sidebarButton.action = @selector(revealToggle:);
_sidebarButton.tintColor = [UIColor colorWithWhite:0.1f alpha:0.7f];
NSUserDefaults *saveapp = [NSUserDefaults standardUserDefaults];
bool saved = [saveapp boolForKey:k_Save];
if (!saved) {
    /// not save code here
} else {
    ///saved code here
    Label.text = @"Item has been purchased.";

// Add pan gesture to hide the sidebar
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
// Do any additional setup after loading the view.
}
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 0 && indexPath.section == 3){
    [[UIApplication sharedApplication] openURL:[NSURL  URLWithString:@"http://howtocrackthedbq.blogspot.com/"]];
} else if(indexPath.row == 1 && indexPath.section == 1){
         if ([MFMailComposeViewController canSendMail]) {
             MFMailComposeViewController *mailViewController =   [[MFMailComposeViewController alloc] init];
             mailViewController.mailComposeDelegate = self;
             [mailViewController setSubject:@"[How to crack the DBQ] Support request"];
             [mailViewController setToRecipients:[NSArray   arrayWithObject:@"moappsco@gmail.com"]];
             [self presentModalViewController:mailViewController animated:YES];
         }
          }

 }

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
[self dismissModalViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} 



-(void)Purchased {
Label.text = @"Item has been purchased.";
NSUserDefaults *saveapp = [NSUserDefaults standardUserDefaults];
[saveapp setBool:TRUE forKey:k_Save];
[saveapp synchronize];
}
- (IBAction)PurchaseItem:(id)sender {

_purchaseController = [[PurchasedViewController alloc] initWithNibName:nil bundle:nil];
_purchaseController.productID = @"com.moappsco.crackingthedbq.dbq1";
[[SKPaymentQueue defaultQueue] addTransactionObserver:_purchaseController];
[self presentViewController:_purchaseController animated:YES completion:NULL];
[_purchaseController getProductID:self];
}
@end

我的PurchasedViewController.h(我的故事板右侧显示的另一个视图控制器的自定义类):

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

@interface PurchasedViewController :    UIViewController<SKPaymentTransactionObserver,SKProductsRequestDelegate> {
}
@property (strong, nonatomic) SKProduct *product;
@property (strong, nonatomic) NSString *productID;
@property (strong, nonatomic) IBOutlet UILabel *productTitle;
@property (strong, nonatomic) IBOutlet UITextView *productDescription;
@property (strong, nonatomic) IBOutlet UIButton *buyButton;

- (IBAction)Restore:(id)sender;
- (IBAction)BuyProduct:(id)sender;
- (IBAction)GoBack:(id)sender;

-(void)getProductID:(UIViewController *)viewController;

@end

我的PurchasedViewController.m:

#import "PurchasedViewController.h"
#import "SettingsViewController.h"

@interface PurchasedViewController ()

@property (strong, nonatomic) SettingsViewController *homeViewController;

@end

@implementation PurchasedViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
_buyButton.enabled = NO;
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)Restore:(id)sender {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

- (IBAction)BuyProduct:(id)sender {
SKPayment *payment = [SKPayment paymentWithProduct:_product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}

- (IBAction)GoBack:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];

}
-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
[self UnlockPurchase];
}

-(void)getProductID:(SettingsViewController *)viewController;{
_homeViewController = viewController;

if([SKPaymentQueue canMakePayments]){
    SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:  [NSSet setWithObject:self.productID]];
    request.delegate = self;
    [request start];
} else
    _productDescription.text = @"Please enable in-app purchases in your settings.";
}


#pragma mark _
#pragma mark SKProductsRequestDelegate

-(void) productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
NSArray *products = response.products;
if (products.count != 0) {
    _product = products[0];
    _buyButton.enabled = YES;
    _productTitle.text = _product.localizedTitle;
    _productDescription.text = _product.localizedDescription;
} else {
    _productTitle.text = @"Product not found.";
}
products = response.invalidProductIdentifiers;

for(SKProduct *product in products) {
    NSLog(@"Product not found: %@", product);
}


}
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions    {
for (SKPaymentTransaction *transaction in transactions) {
    switch (transaction.transactionState) {
        case SKPaymentTransactionStatePurchased:[self UnlockPurchase];
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            break;
        case SKPaymentTransactionStateFailed:NSLog(@"Transaction Failed");
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
        default:
            break;

    }
}
}

-(void)UnlockPurchase {
_buyButton.enabled = NO;
[_buyButton setTitle:@"Purchased" forState:UIControlStateDisabled];
[_homeViewController Purchased];
}
@end

如果需要,您可以在Github上下载我的xcode项目:https://github.com/MoAppsCo/iOS-Apps/tree/master/How%20to%20crack%20the%20DBQ

1 个答案:

答案 0 :(得分:1)

您没有正确实例化您的控制器。当控制器在故事板中时,您不应该使用initWithNibName:bundle :(这是基于xib的控制器),您应该在故事板中为控制器提供标识符,并使用:

_purchaseController = [self.storyboard instantiateViewControllerWithIdentifier:@"YourNameHere"];

当然,您也可以通过将segue连接到该控制器,从设置控制器进行操作,然后使用performSegueWithIdentifier:sender:而不是。