将变量从子视图传递到Swift中的父视图

时间:2015-08-19 13:54:28

标签: swift parent-child key-value-observing

我尝试使用setter和getter函数将子视图(inputPopUp.swift)中的变量传递给父视图。这是我孩子视图中的代码:

var char: String!
var buttonPressed = String()

@IBAction func addButtonPressed(sender: AnyObject) {
    buttonPressed = "addButton"
    setChar("+")
    getChar()
    self.removeAnimate()
}

@IBAction func minusButtonPressed(sender: AnyObject) {
    buttonPressed = "minusButton"
    setChar("-")
    self.removeAnimate()
}

@IBAction func divideButtonPressed(sender: AnyObject) {
    buttonPressed = "divideButton"
    setChar("/")
    self.removeAnimate()
}

@IBAction func multiplyButtonPressed(sender: AnyObject) {
    buttonPressed = "multiplyButton"
    setChar("*")
    self.removeAnimate()
}

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

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

我正在尝试访问' char'我的父视图中的变量是这样的,但它返回nil - 大概是因为我正在调用函数的新实例。

@IBAction func runButtonPressed(sender: AnyObject) {
    inputPopUp().getChar()
} 

如何有效地将数据从孩子传递给父母?

我还希望根据子视图中的按钮单击更新父视图中的标签。我正在尝试实施键值观察。这是我到目前为止所得到的。

class ObserveChar: NSObject {

dynamic var char = String()

func updateChar(){

    char = String()
}

}

private var myContext = 0

class Observer: NSObject {

var objectToObserve = ObserveChar()

override init(){

    super.init()

    objectToObserve.addObserver(self, forKeyPath: "char", options: .New, context: &myContext)
}

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {

    if context == &myContext {

        println("Char updated: \(change[NSKeyValueChangeNewKey])")

    } else {
        super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
    }
}

deinit {

    objectToObserve.removeObserver(self, forKeyPath: "char", context: &myContext)

}

}

2 个答案:

答案 0 :(得分:1)

您的项目似乎与计算器有关。你不应该使用

ParentView().tapLabel.text = getChar() 因为,您已多次创建parentView,您应将parentView设置为其弱属性

您还可以使用KVO观察子视图的char变量;或者您也可以使用NSNotification更新父视图的tapLabel;或者您也可以使用委托。 代表喜欢以下

protocol UpdateTapLabel {
func didTapSomeOperateAction(operateInfo : String)

}

class ParentClass : NSObject, UpdateTapLabel {
var tapLabel : UILabel!
var childView : ChildView!
func didTapSomeOperateAction(operateInfo: String) {
    self.tapLabel.text = operateInfo
}
//you should initialize the child view like this
/*
childView = ChildView()
childView.delegate = self
*/

}

class ChildView: NSObject {
var char: String!
var delegate : UpdateTapLabel
//getter method
func getChar() {
    if (buttonPressed == "addButton"){
        char = "+"
    }
    if (buttonPressed == "minusButton"){
        char = "-"
    }
    if (buttonPressed == "divideButton"){
        char = "/"
    }
    if (buttonPressed == "multiplyButton"){
        char = "*"
    }
    self.delegate.didTapSomeOperateAction(char)
}

}

KVO喜欢以下

//you should add observer like this
//self.addObserver(self, forKeyPath: "childView.char", options: NSKeyValueObservingOptions.New, context: nil)
//also you should remove it in deinit, otherwise you will get crash when release parentView
deinit {
    self.removeObserver(self, forKeyPath: "childView.char")
}
//you can handle the kvo here
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
    if keyPath == "childView.char" {
        self.tapLabel.text = change[NSKeyValueChangeNewKey]
    }
}

答案 1 :(得分:0)

好的,也许您可​​以将子视图设置为父视图的属性,也许就像这样

var inputPopView : inputPopUp       /**<initialize it somewhere */

然后你可以像这样使用它

@IBAction func runButtonPressed(sender: AnyObject) {
self.inputPopView.getChar() 
}