2

I have got two views. first ist "homeView" the second is "detailView".

in homeView i have two buttons button1 button2

in detailView i have on label label1

when i push button1 the should change to "detailView" an the label1.text should be "button 1 gedrückt" when i push button2 the should change to "detailView" an the label1.text should be "button 2 gedrückt"

when i use the code:

@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!

@IBAction func button1(sender: UIButton) {

    let vc : AnyObject! = self.storyboard!.instantiateViewControllerWithIdentifier("detailview")
    self.showViewController(vc as! UIViewController, sender: vc)


    // self.label1.text = "button 1 gedrückt"

}

the view will change, but the label1.text won´t change. if i use

@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!

@IBAction func button1(sender: UIButton) {

    let vc : AnyObject! = self.storyboard!.instantiateViewControllerWithIdentifier("detailview")
    self.showViewController(vc as! UIViewController, sender: vc)


    self.label1.text = "button 1 gedrückt"

}

i will get an error: enter image description here

i don´t know what i can do??

rmaddy
  • 314,917
  • 42
  • 532
  • 579
euleec7
  • 55
  • 1
  • 6
  • 1
    Check the connection to `label1` in your storyboard. The probable cause of your problem is that it isn't connected. The property is an implicitly de-referenced optional, so it will crash if you try to access it when it is not connected – Rich Tolley Sep 16 '15 at 20:03

2 Answers2

3

Make sure your label is connected to your outlet or if it is duplicated. If its okay try removing the connection and connect it again. The image bellow ilustrate what im talking about.

enter image description here

adolfosrs
  • 9,286
  • 5
  • 39
  • 67
0

Thanks too all. now i can change the text in label1 with this code

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "segueTest") {
        let svc = segue.destinationViewController as! SecondViewController;

        svc.toPass = label2.text

    }
}

now another question? can i put this "override func" into IBAction? because i want to add more buttons. with every button there should be another text in the label1.

Thanks (i´m an absolut beginner in Xcode and swift)

euleec7
  • 55
  • 1
  • 6
  • actually not. prepareForSegue will be called everytime you segue to another view. there is no need to put it into an IBAction. Try oppening another question in stack overflow detalling a little better whats going on so we can solve your issues. – adolfosrs Sep 17 '15 at 19:09