last day (15 days later) » 

6:31 PM
0
A: PopUpViewController won't pop up more then once. SWIFT

Jacobson TalomYour 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 Questi...

 
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.
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.
 
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.
 
the popUpViewController is very relevant, its the cause and problem to why it's not working.
the problem is to do with how the popUpViewController is being called and made childViewController. I think?
 
You are telling me all the code in ur PopupViewController is relevant?
 
Maybe not all, I have simplified slightly.
 
6:31 PM
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.
 
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.
 
I updated the answer.
 
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?
 
updated again, if you want we can start a chat so we won't make to many comments.
 
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.
 
6:31 PM
Hey, I've created a start room so I can explain a few things. You ok with it?
 
Yes certainly
 
var questionsGameVC = QuestionsGame() just creates another instance, so it's basically the same as before
what you want to is to access the method "matched()" in your original instance
you can do that by using a delegate (updated in my answer). I'm pretty sure that will solve the problem
 
ok, thanks for your help. I'll look into a delegate...Just not to confident in doing them yet
 
if you have questions, you can just ask them here. I can help you anytime, since I have to wait for something for another hour... :p
 
Ok well, I am looking at your delegate link you have directed me to get a better understanding
so the popUpViewController will become the delegate of the questionsVC?
Will the protocol go inside the popUpViewController?
 
6:41 PM
lets see
well, the protocol itself is the delegate
in your case it would be something like:
protocol QuestionGameDelegate {
oh shit, how to post code here :D
 
Just type it in, I should be able to work it out
 
protocol QuestionGameDelegate: class {
    func matched()
}

class QuestionsGame: UIViewController, QuestionGameDelegate {

    var popUpViewController = PopUpViewControllerSwift()

    // your code

    func matched() {
        popUpViewControllerSwift.delegate = self

        // your code
    }
}

@objc class PopUpViewControllerSwift : UIViewController {
    weak var delegate: QuestionGameDelegate?

    func playAgain() {
        delegate?.matched()
    }
}
 
Ahh ok so the delegate is working, it is running the matched() function, but the popUpView is still not showing!?
the function runs but it still doesnt pop up :(
are you sure it is nothing to do with the self.addChildViewController(self.popUpViewController) or the self.popUpViewController.didMoveToParentViewController(self)
 
hmm let me try to make something similar
 
6:56 PM
ok thanks
 
7:16 PM
it's working fine for me :O
does your popup has to be the same?
as in, the same instance?
 
does it have to be the same? or isit the same?
 
in my own test, im creating a new instance every time you create a new popover
which should be
 
I am not sure, it just created the way I have shown you in my code
how have you done it?
 
protocol QuestionGameDelegate: class {
    func matched()
}

var alreadyMatched = [0,1,2]

class QuestionsGame: UIViewController, QuestionGameDelegate {

    var popUpViewController: popupViewControllerSwift!

    override func viewDidLoad() {
        super.viewDidLoad()

        matched()
    }


    func matched() {

        var a = alreadyMatched.count
        if a > 0 {
            popUpViewController = storyboard?.instantiateViewControllerWithIdentifier("popup") as! popupViewControllerSwift
this is in QuestionGame.swift
class popupViewControllerSwift: UIViewController {

    @IBOutlet weak var label: UILabel!

    var number: Int!

    weak var delegate: QuestionGameDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

        label.text = String(number)

    }

    @IBAction func button(sender: UIButton) {
        delegate?.matched()
    }
}
this is in PopupViewController.swift
i have a label and a button there. If i click the button, it will access the method "matched()" in QuestionGame. The label shows the count of your array.
 
see you have done it different, inside the matched function and inside the if statement you need to put:
self.view.addSubview(self.popUpViewController.view)
self.addChildViewController(self.popUpViewController)
self.popUpViewController.setValues(UIImage(named: "hot.png"), messageText: "You have matched with \(alreadyMatched[index])!", congratsText: "Snap!")
self.popUpViewController.didMoveToParentViewController(self)
is this correct?
 
7:24 PM
i have, you need to open the "view full text" to see the full code
i removed the values for your UIImage though
why are you adding it as a child though?
you only need to add as a child if you're using your view controller as a container view
 
ahh ok sorry my bad I didnt see it all, ok give me a minute to try that
You know you have put storyboard?.instantiateViewControllerWithIdentifier("popup") as! popupViewControllerSwift
 
if you just want to show a popover, you can also just use:
func matched() {

        // Create PopoverViewController
        let popoverViewController = UIStoryboard.mainStoryboard().instantiateViewControllerWithIdentifier("popover") as! PopoverViewController
        popoverViewController.modalPresentationStyle = .Popover
        popoverViewController.delegate = self
        popoverViewController.preferredContentSize = CGSize(width: 200, height: 260)

        // Settings for the popover
        let popover = popoverViewController.popoverPresentationController!
 
I have created this in code, there is no storyboard viewcontroller
 
ye, thats why i asked whether you want it to be the same instance or a new one every time you click on the button
 
I just want it to work!? lol
 
7:30 PM
what do you mean, no storyboard view controller?
hehe, its quite easy tbh :p
the string "popover" is the name you have to set as identifier in your storyboard
 
but there is no popOverViewController in the storyboard, it is all created via code, as I originally showed you
it is created as a UIView
 
ok, thats no problem. its just a way of creating an instance of a viewcontroller
 
my popUpView is created like so:
self.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)
self.popUpView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width - 40, self.view.frame.size.height - 120))
popUpView.center = self.view.center
self.popUpView.layer.cornerRadius = 5
self.popUpView.layer.shadowOpacity = 0.8
self.popUpView.backgroundColor = UIColor.whiteColor()
self.popUpView.layer.shadowOffset = CGSizeMake(0.0, 0.0)
view.addSubview(popUpView)
this is inside the viewDidLoad of the PopUpViewControllerSwift
 
sec, trying something
 
ok cool
 
7:44 PM
try my new code, ill look into yours
replace the code where i create the popover with: let popUpViewController = PopupViewControllerSwift()
and the code for PopupViewController:
class PopupViewControllerSwift: UIViewController {

    var label: UILabel!
    var button: UIButton!

    var number: Int! {
        didSet {
            label.text = String(number)
        }
    }

    weak var delegate: QuestionGameDelegate?

    init() {
        super.init(nibName: nil, bundle: nil)

        label = UILabel(frame: CGRectMake(100, 100, 50, 25))
        label.backgroundColor = UIColor.redColor()
        label.textAlignment = .Center
        view.addSubview(label)

        button = UIButton(frame: CGRectMake(200, 200, 100, 50))
this way you can test it and see whats wrong
 
This is basically what I originally had, it still does not work.
how do you place code inside here?
like you have done it ^^^
and I will show you what I have done so you have a better understanding
var alreadyMatched = [String]()

class QuestionsGame: UIViewController, popUpDel {
 
i just copied the code from xcode
and paste it here
 
ok give me a minute
ok so the questionsVC:
var alreadyMatched = [1,2,3]

class QuestionsGame: UIViewController, popUpDel {

    var popUpViewController = PopUpViewControllerSwift()

    override func viewDidLoad() {
        super.viewDidLoad()

       matched()
    }

    func matched(){

        var a = alreadyMatched.count


        if a > 0 {

            println("Runsn here fine")
            popUpViewController.delegate = self
            self.view.addSubview(self.popUpViewController.view)
            self.addChildViewController(self.popUpViewController)
and the popUpViewController:
 protocol popUpDel: class {
        func matched()
    }

    @objc class PopUpViewControllerSwift : UIViewController {

         weak var delegate: popUpDel?
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        popUpViewSetUp()
    }

    func popUpViewSetUp() {

        self.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)
        self.popUpView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width - 40, self.view.frame.size.height - 120))
 
7:59 PM
ok lets see
 
sorry it should say delegate.matched() inside the func playAgain
 
8:14 PM
i think i know the reason why it only show up once
because you remove the view of the popup from itself, so there is no view to add for the second time
although, the whole thing is a bit weird to use a popover
 
Ah ok, I see. My reason for displaying the pop over this way is because when I displayed it normally, the buttons did not work, it just caused a crash. This was the only way I could get it to work with the buttons works aswell.
 
hmm that shouldnt be the case, popover is just another viewcontroller
besides, the popoverviewcontroller is still inside question game view controller as a child etc
 
no I had to make it a child for it to work
 
ye, adding is not a problem, but right now, theres no way in deleting it
 
ok so what do you suggest?
 
8:20 PM
if its just a popup you want, you can try using a popoverPresentationController
 
That means recreating everything, I am happy with the way it alll works as a animation etc, I just wanted to be able to solve this problem really
is there no simple solution for it?
 
hmm ill try to make it work
 
if I was to create it as a popoverPresentationController, would it still animate the same and look the same etc?
 
8:37 PM
ok got it
the code looks a bit messy though
well, you can configure the popover making it look the same
but let me show you the code first
the call for the delegate, i moved it to "removeAnimate()"
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.delegate?.matched()
                }
        })
    }
and for the code of QuestionGame:
var alreadyMatched = [1,2,3]

class QuestionsGame: UIViewController, popUpDel {

    var pendingViewController: PopUpViewControllerSwift? {
        didSet {
            if let pending = pendingViewController {
                pending.delegate = self
                addChildViewController(pending)
                view.addSubview(pending.view)
                pending.didMoveToParentViewController(self)
                println("tekst")
            }
        }
    }
    var activeViewController: UIViewController?
 
ok so the self.view.removeFromSuperview() inside the fun removeAnimate i should take that out and replace with self.delegate?.matched()?
This works! apart from this doesn't allow me to have the initializers: self.popUpViewController.setValues(UIImage(named: "hot.png"), messageText: "You have matched with (alreadyMatched[index])!", congratsText: "Snap!")
So there is no images and messageText etc
 
you should be able to add it though
i just removed it, to make the code simpler
 
8:56 PM
but how, there are so many different viewControllers created now its hard to tell what is actually going on lol
 
there are only 2 different view controllers, should add it to the popoverviewcontroller like before
 
 func usersHaveMatched(){

        var a = alreadyMatched.count
        var index = a - 1

        if a > 0 {

            removeViewController(activeViewController)
            pendingViewController = PopUpViewControllerSwift()
            activeViewController = pendingViewController
            self.popUpViewController.setValues(UIImage(named: "hot.png"), messageText: "You have matched with \(alreadyMatched[index])!", congratsText: "Snap!")
            alreadyMatched.removeLast()
            println(a)
I have added it like this and it is causing a crash
it needs to be added into the questionsVC as the image and message will be different for every time it pops up
 
var pendingViewController: PopUpViewControllerSwift? {
        didSet {
            if let pending = pendingViewController {
                pending.delegate = self
                pending.setValues(UIImage(named: "hot.png"), messageText: "You have matched!", congratsText: "Snap!")
                addChildViewController(pending)
                view.addSubview(pending.view)
                pending.didMoveToParentViewController(self)
            }
        }
    }
add it there :)
ah ok wait
 
that still crashes
 
func matched() {

        var a = alreadyMatched.count

        if a > 0 {
            removeViewController(activeViewController)
            let popup = PopUpViewControllerSwift()
            popup.setValues(UIImage(named: "hot.png"), messageText: "You have matched!", congratsText: "Snap!")
            pendingViewController = popup
            activeViewController = pendingViewController
            alreadyMatched.removeLast()
            println(a)
            println(view.subviews.count)

        }
forget the change in pendingviewcontroller if you need to change the values every time
if the labels and image causes a crash (from not being nil) you have to create the labels and imageviews in the init() initializer
 
9:05 PM
yes its still causing a crash saying: unexpectedly found nil while unwrapping an Optional value
what init() initializer?
 
yea thought so
im not sure where you create the labels and images
 
hmmm :/
 
but i don't see an init() in your code. it has something to do with the lifecycle of a viewcontroller
anyway, add this to your code in popoverViewController:
init() {
        super.init(nibName: nil, bundle: nil)

        // create the labels here
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
ok but how do I create them, like I said Each image and messgeText will be different?
I gather each different image and text inside the questionsVC
 
thats ok, in the init you just have to create the labels
and UIImage
you can set the text and image just you used to did
but creating the labels and imageviews in init(), you'll make sure they're created before changing the text and images
 
9:15 PM
oh ok, but how do I set create them in the init? lol I've never had to do this before
 
lol eh
 
you have put: // create the labels here
 
init() {
        super.init(nibName: nil, bundle: nil)

        messageLabel = UILabel(frame: CGRectMake(10, 10, 50, 20))
        congratsLabel = UILabel(frame: CGRectMake(10, 40, 50, 20))
        popUpUserImage = UIImage()
    }
 
ohhh ok
I get it now, sorry I thought you meant something different
so can I take them out of the popUpViewSet() then?
 
ye
or wait, let me check first :p
you can put popupviewsetup in init()
which is the same, or even better
because with PopupViewController(), init() comes before viewDidLoad
 
9:28 PM
sorry i'm really lost, it's still not working. I'm not really sure whats going wrong? lol
 
whats the crash message now? :p
init() {
        super.init(nibName: nil, bundle: nil)

        popUpViewSetUp()
    }
this should be enough
but i don't see anything about the popUpUserImage and congratsLabel in popUpViewSetUp()
 
I left it out to make the code easier for you.
now it is not crashing but the pop up only has the buttons on
no image or messageLabel or congratsLabe
should I be called popUpViewController.setValues in the questionsVC still?
 
ye
func matched() {

        var a = alreadyMatched.count

        if a > 0 {
            removeViewController(activeViewController)
            let popup = PopUpViewControllerSwift()
            popup.setValues(UIImage(named: "hot.png"), messageText: "You have matched!", congratsText: "Snap!")
            pendingViewController = popup
            activeViewController = pendingViewController
            alreadyMatched.removeLast()
            println(a)
            println(view.subviews.count)

        }
still like this
 
nope still nothing
pop up view shows but nothing on it
so frustrating
 
haha
can you give me the full code?
then ill fix it for you
 
9:40 PM
ok popUpViewController:
@objc class PopUpViewControllerSwift : UIViewController {


    init() {
        super.init(nibName: nil, bundle: nil)

        popUpViewSetUp()
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

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

    weak var delegate: popUpDel?


    var matchedUser : PFUser!

    override func viewDidLoad() {
and the questionsVC:
var alreadyMatched = [1,2,3]

class QuestionsGame: UIViewController, popUpDel {

    var popUpViewController = PopUpViewControllerSwift()
    var activeViewController: UIViewController?

    override func viewDidLoad() {
        super.viewDidLoad()

       matched()
    }

    var pendingViewController: PopUpViewControllerSwift? {
        didSet {
            if let pending = pendingViewController {
                pending.delegate = self
                addChildViewController(pending)
                view.addSubview(pending.view)
 
copied your code (removed some things like the font you're using and PFUser)
and its working, although the text stays the same because the text isn't changing in QuestionGame
class QuestionsGame: UIViewController, popUpDel {

    var pendingViewController: PopUpViewControllerSwift? {
        didSet {
            if let pending = pendingViewController {
                pending.delegate = self
                addChildViewController(pending)
                view.addSubview(pending.view)
                pending.didMoveToParentViewController(self)
            }
        }
    }
    var activeViewController: UIViewController?

    override func viewDidLoad() {
        super.viewDidLoad()
try this for your QuestionGame
 
it's exactly the same is it not?
 
well, its working here. except for the part of the text which should be different every time no?
 
it works!!!
haha wow
finally thank you so much! could never of done that without you
 
haha
great :)
you're welcome, its nice fixing problems :)
 
9:56 PM
my only question is that it says there are 8 subviews?
on the println(view.subviews.count)
is that normal? it seems alot?
 
strange, in my output, it says 3 as max
are you using my code? :P
can you show me the code you have in QuestionsGame?
 
yes I'm using your code, but I have alot more code inside QuestionsGame, I only showed you what was relevent
 
if you only use removeViewCOntroller() and pendingViewController, then the subviews count should always be controlled perfectly
unless you have other things of course
does it stay at 8 or is it increasing?
because the subviews can be buttons/labels and other things
 
it just says 8
it stays at 8
although there are other subviews on that view controller e.g. other labels and buttons etc so that must be it correct?
sorry my bad
 
ye, then its correct :)
as long as it doesn't increase, then it means its working just fine
if this helped, pls mark my answer as answered so i get points :)
 
10:04 PM
ok brilliant
well thanks for all your help
 
no problem, im glad i could be of any help :)
 
maybe we'll meet again on here one day, I'm always seek for help so I am sure
cya soon!
 
heh, bye! don't forget to mark my answer! :P
 

last day (15 days later) »