使用Storyboard呈现视图控制器

时间:2013-12-18 17:50:17

标签: ios objective-c storyboard

我已经按照教程将In App Purchase添加到我的应用中。有2次观看次数:

  1. 按钮“购买商品”
  2. 屏幕出现,允许用户选择产品
  3. 我已经完全添加了代码但在教程中他们使用的是XIB文件,但我使用的是Storyboard。我的“购买项目”按钮的代码如下所示:

    - (IBAction)PurchaseItem:(id)sender {
    
        _purchaseController = [[PurchasedViewController alloc] initWithNibName:Nil bundle:nil];
    
        _purchaseController.productID = @"com.myapp";
    
        [self presentViewController:_purchaseController animated:YES completion:NULL];
    
        [_purchaseController getProductID:self];
    
    }
    

    我遇到的问题是,当点击按钮时,会出现黑屏,但我想要BuysedViewController显示

    我需要改变什么吗?

    编辑:

    使用已编辑的代码但收到错误:

    - (IBAction)PurchaseItem:(id)sender {
    
            PurchasedViewController *purchaseContr = (PurchasedViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"menu"];
            //menu is only an example
            purchaseContr.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
            [self presentViewController:purchaseContr animated:YES completion:nil];
    
        }
    

    enter image description here

2 个答案:

答案 0 :(得分:19)

使用故事板,你应该给出如图中的标识符: 点击你的viewController和'身份检查员'

enter image description here

在此示例中,自定义类应为:PurchasedViewController

这是代码:

 - (IBAction)PurchaseItem:(id)sender {
     PurchasedViewController *purchaseContr = (PurchasedViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"menu"];
     //menu is only an example
     purchaseContr.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
     [self presentViewController:purchaseContr animated:YES completion:nil];
 }

答案 1 :(得分:0)

试试这个

_purchaseController = [[PurchasedViewController alloc] init];

_purchaseController.productID = @"com.myapp";        
/* 1. Add the new VC as a child VC */
[self addChildViewController:_purchaseController];

/* 2. Add new view to source view */
[self.view addSubview:_purchaseController.view];

 /* 3. Inform 'didMoveToParentViewController' to newly added view controller */
[_purchaseController  didMoveToParentViewController:self];

[_purchaseController getProductID:self];