0

I am trying to update a label in my parent view (JL1EX3.swift) based on a buttonPressed event from my child view (inputPopUp.swift). I am aware I need to implement delegates/protocols for this to happen but am clueless as how to do so. Here's the code within my child view:

weak var parentView: JL1EX3!
var char: String!
var buttonPressed = String()

//Setter 
func setChar(var thisChar: String){
    char = thisChar
}

//Getter
func getChar()-> String{
    if (buttonPressed == "addButton"){
        char = "+"
    }
    if (buttonPressed == "minusButton"){
        char = "-"
    }
    if (buttonPressed == "divideButton"){
        char = "/"
    }
    if (buttonPressed == "multiplyButton"){
        char = "*"
    }
    println(char)
    return char
}


@IBAction func addButtonPressed(sender: AnyObject) {
    buttonPressed = "addButton"
    setChar("+")
    getChar()
    self.parentView.tapLabel.text = getChar() //Does not work - returns nil
    self.removeAnimate()
}

Your help would be very much appreciated!!

2
  • Where do you assign your parentView? Commented Aug 19, 2015 at 19:37
  • I thought I assigned it on the first line, no? weak var parentView: JL1EX3! Commented Aug 19, 2015 at 20:53

1 Answer 1

0
weak var parentView: JL1EX3!

does not assign a parent view. All this line does is declares a variable which is of class JL1EX3. It doesn't create any references to any particular instance of class JL1EX3.

Firstly, I strongly discourage you from using implicit unwrapping. Better declare your parent view like

weak var parentView: JL1EX3?

Secondly, ensure that you assign the parentView variable. You should do this after you initialise your inputPopUp.

For example:

let myPopUp = inputPopUp()
myPopUp.parentView = self //this assigns the parentView. Otherwise it is nil

I hope this helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.