0

I have PopUpViewControllerSwift which I want to pop up time after time until the an alreadyMatched index reaches zero. This is how I am performing the pop up, code:

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()
            }
        }
}

and the PopUpViewControllerSwiftcode is:

@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()

                }
        })
    }
}

For some reason this will only pop up one time, no repeatedly? I am sure it is something to do with the ParentViewController?

Henry Brown
  • 2,219
  • 8
  • 31
  • 48

1 Answers1

0

Your first instance of QuestionsGame is showing in the screen. In viewDidLoad, you execute matched() and put the view of your popover in this instance, which shows the popover. This works correctly.

Now the part where you want to show it again: You have a button creating a new instance of QuestionsGame with the code:

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

you accessed matched() manually again (which results it in being called twice, one in viewDidLoad and one manually) This method will put the view of your popover in the new instance, but you won't see all this because the new instance is not in view.

EDIT:

If you want to create a new popup, using a delegate would be a simple way of doing it. I've explained the use of a delegate in another answer. I'm not sure how you are dismissing your popover though, but that's another problem. If you want to create a new popover within your popover, you can use a delegate to access the method matched() so the new popover will be shown on top of your current view.

Community
  • 1
  • 1
Eendje
  • 8,815
  • 1
  • 29
  • 31
  • Sorry thats not correct. a will be 3, but then using the `alreadyMatched.removeLast()` will make it become 2. Which is still more then 0. The if statement runs fine, the problem is the view controller will not run more then once. – Henry Brown Apr 18 '15 at 17:29
  • No because if you read the code, the function `matched()` is called again once the popUpViewController is removed from the view, there for repeating the process until the count reaches 0. no for in loop is needed. – Henry Brown Apr 18 '15 at 17:35
  • Hmm, didn't read the code of your popover class. You really should remove code thats not relevant to your question (and fix the markup). Makes it more readable for people to help you. – Eendje Apr 18 '15 at 17:37
  • the popUpViewController is very relevant, its the cause and problem to why it's not working. – Henry Brown Apr 18 '15 at 17:38
  • the problem is to do with how the popUpViewController is being called and made childViewController. I think? – Henry Brown Apr 18 '15 at 17:39
  • You are telling me all the code in ur PopupViewController is relevant? – Eendje Apr 18 '15 at 17:40
  • Maybe not all, I have simplified slightly. – Henry Brown Apr 18 '15 at 17:41
  • So I've used the search function to find where you've used `matched()`. Thats when the playagain button is pressed no? I think the problem lies in calling the method `matched()`. You're making a new instance of `QuestionsGame` and putting the view of your popup in that new instance, while the new instance of `QuestionsGame` is not in view. – Eendje Apr 18 '15 at 17:45
  • Yes it is inside the playAgain Button. why would the questionsGame not be in view? the popUpViewController is just animated over the top right? then it is removed? then I am trying to show it again. – Henry Brown Apr 18 '15 at 17:49
  • Ahh Ok, its starting to make more sense now. So this is the answer, but what is the solution? how might I write the code so it will work? – Henry Brown Apr 18 '15 at 18:19
  • updated again, if you want we can start a chat so we won't make to many comments. – Eendje Apr 18 '15 at 18:29
  • I still think that hasnt solved the problem, as I have now called the `matched()` function like this: `var questionsGameVC = QuestionsGame() questionsGameVC.matched()` and still nothing happenes, although the function is being called as `println` statements are being printed. – Henry Brown Apr 18 '15 at 18:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75595/discussion-between-jacobson-talom-and-henry-brown). – Eendje Apr 18 '15 at 18:30