lundi 29 juin 2015

how to acces a variable in another class and change its value


I am new to Xcode and Swift so I don't know much about how it all works, but I am trying to make a pop-up view. I want a small view to pop up when I click a button. The view is a View Container (I don't know if that is the best way to do this so if not please tell me a better way to do this) and it starts out hidden then when I click a button it becomes visible. This View Container also has a button that if clicked, it will make the view hidden again.

Here is the code:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBOutlet weak var popUpView: UIView!

    @IBAction func startButton(sender: UIButton) {
        popUpView.hidden = false
    }

}

import UIKit

class PopUpViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
       // Dispose of any resources that can be recreated.
    }

    override func  prepareForSegue(segue:UIStoryboardSegue,
                                   sender:AnyObject?) 
    {
       // Get the new view controller using segue.destinationViewController.
       // Pass the selected object to the new view controller.
    }

    @IBAction func backButton(sender: UIButton) {
        ViewController().popUpView.hidden = true
    }


}

When I run the app it starts fine because the start button is there and when I click it the pop up shows up but when I click the back button it gives me an error which says that in the console

Unknown class MKMapView in Interface Builder file. fatal error: unexpectedly found nil while unwrapping an Optional value

and in line 31 ViewControler().popUpView.hidden = true

it says Thread 1: EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP, subcode=0x0)

Can someone help. Thanks


Aucun commentaire:

Enregistrer un commentaire