0

我有PopUpViewControllerSwift我想一次又一次地弹出,直到alreadyMatched索引达到零。这就是我执行弹出窗口的方式,代码:

var alreadyMatched = [0,1,2]   

class QuestionsGame: UIViewController {

         var popUpViewController = PopUpViewControllerSwift()

   override func viewDidLoad() {
    super.viewDidLoad()

       matched()
}


    func matched() { 

        var a = alreadyMatched.count   
        if a > 0 {

            self.view.addSubview(self.popUpViewController.view)
            self.addChildViewController(self.popUpViewController)
            self.popUpViewController.setValues(UIImage(named: "hot.png"), messageText: "You have matched!!", congratsText: "Snap!")
            self.popUpViewController.didMoveToParentViewController(self)
            alreadyMatched.removeLast()
            }
        }
}

PopUpViewControllerSwift代码是:

@objc class PopUpViewControllerSwift : UIViewController {

    var popUpUserImage: UIImageView!
    var messageLabel: UILabel!
    var popUpView: UIView!
    var congratsLabel: UILabel!
    var matchedOrNot = 2

    var matchedUser : PFUser!

    override func viewDidLoad() {
        super.viewDidLoad()

    }


    func setValues(image : UIImage!, messageText : String, congratsText : String) {
        self.popUpUserImage!.image = image
        self.messageLabel!.text = messageText
        self.congratsLabel.text = congratsText
    }

    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()
                    let sb = UIStoryboard(name: "Main", bundle: nil)
                      let questionsVC =   sb.instantiateViewControllerWithIdentifier("Questions") as! QuestionsGame
                      questionsVC.timer()

                }
        })
    }
}

由于某种原因,这只会弹出一次,不会重复?我确定这与 ParentViewController 有关吗?

4

1 回答 1

0

您的第一个实例QuestionsGame正在屏幕中显示。在 viewDidLoad 中,您执行matched()并将弹出框的视图放入此实例中,该实例显示了弹出框。这可以正常工作。

现在您要再次显示它的部分:您有一个按钮,使用代码创建一个实例:QuestionsGame

let questionsVC = sb.instantiateViewControllerWithIdentifier("Questions") as! QuestionsGame

matched()再次手动访问(导致它被调用两次,一次在 viewDidLoad 中,一次手动)此方法会将弹出窗口的视图放在实例中,但您不会看到所有这些,因为实例不在视图中.

编辑:

如果您想创建一个新的弹出窗口,使用委托将是一种简单的方法。我已经在另一个答案中解释了委托的使用。我不确定您是如何消除弹出窗口的,但这是另一个问题。如果你想在你的弹出框内创建一个新的弹出框,你可以使用一个委托来访问该方法matched(),这样新的弹出框将显示在你当前视图的顶部。

于 2015-04-18T17:25:13.337 回答