我正在看这个教程
youtube,我在弹出窗口中添加了按钮.按钮是添加硬币(IAP),但问题是当我解除弹出时它不会刷新视图以查看更新硬币.如果我按回按钮然后再来它会更新.
所以问题是按下关闭按钮后如何刷新父视图.
这是popupVC
import UIKit class PopUpViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.8) self.showAnimate() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func closePopUp(sender: AnyObject) { self.removeAnimate() //HERE SHOULD BE CODE TO RELOAD PARENT VIEWCONTROLER } func showAnimate() { self.view.transform = CGAffineTransformMakeScale(1.3,1.3) self.view.alpha = 0.0; UIView.animateWithDuration(0.25,animations: { self.view.alpha = 1.0 self.view.transform = CGAffineTransformMakeScale(1.0,1.0) }); } func removeAnimate() { UIView.animateWithDuration(0.25,animations: { self.view.transform = CGAffineTransformMakeScale(1.3,1.3) self.view.alpha = 0.0; },completion:{(finished : Bool) in if (finished) { self.view.removeFromSuperview() } }); @IBAction func btnAddCoins(sender: UIButton) { for product in list { let prodID = product.productIdentifier if(prodID == "XXXX") { p = product buyProduct() break; } } } }
我没有复制所有IAP代码.
解决方法
一个简单的方法是使用委托来处理这个问题.我们的想法是,当您创建PopUpViewController时,您将为其分配委托.这意味着父VC符合委托.
代表可能是什么的一个例子.请注意,建议支持处理成功,失败和取消的方法.
此代码均未经过编译测试,仅供参考.
protocol IAPDelegate { func purchaseSuccessful() func purchaseCancelled() func purchaseFailed() }
你的班级会改成这样的东西.我省略了你的一堆代码.您还需要找出处理purchaseCancelled和purchaseFailed的位置.此外,您可能希望为呼叫提供更多信息(即参数),但我还是要由您决定.
class PopUpViewController: UIViewController { weak var iapDelegate : IAPDelegate? // Your other code here @IBAction func closePopUp(sender: AnyObject) { self.removeAnimate() iapDelegate?.purchaseSuccessful() } }
您的父VC,我在这里调用ParentViewController会是这样的
class ParentViewController: UIViewController,IAPDelegate
顺便说一下,你应该想到更好的课程命名.如果PopUpViewController确实是您的IAP购买VC,那么请将其命名为.如果你想要另一个弹出视图控制器怎么办?你有什么名字呢?