PopUpViewController不会弹出超过一次。迅速

时间:2015-04-18 16:52:04

标签: swift uiviewcontroller popup childviewcontroller parentviewcontroller

我有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有关吗?

1 个答案:

答案 0 :(得分:0)

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

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

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

您再次手动访问matched()(这导致它被调用两次,一次在viewDidLoad中,一次在手动中)此方法会将您的popover视图放在 new 实例中,但是你不会看到这一切,因为实例不在视野中。

编辑:

如果要创建新的弹出窗口,使用委托将是一种简单的方法。我已经解释了在另一个answer中使用代理人的问题。我不确定你是如何解雇你的弹出窗口,但这是另一个问题。如果要在弹出窗口中创建新的弹出窗口,可以使用委托来访问方法matched(),以便新的弹出窗口显示在当前视图的顶部。