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

Pass variable from child view to parent view in Swift

我正在尝试使用 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)

}

}

好吧,也许你可以把子视图设置成父视图的属性,可能像这样

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

然后你可以像这样使用它

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

看来你的项目是关于计算器的。你不应该使用

ParentView().tapLabel.text = getChar() 因为,你已经多次创建了parentView,你应该将parentView设置为它的weak 属性

你也可以使用KVO来观察子视图的char变量;或者你也可以使用NSNotification来更新父视图的tapLabel;或者你也可以使用delegate。 代表点赞如下

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]
    }
}