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

在Swift中将变量从子视图传递到父视图,swift,parent-child,key-value-observing,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.removeAn

我试图使用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
类观察者:NSObject{
var objectToObserve=ObserveChar()
重写init(){
super.init()
objectToObserve.addObserver(self,forKeyPath:“char”,选项:。新建,上下文:&myContext)
}
重写func observeValueForKeyPath(键路径:字符串,对象对象:AnyObject,更改:[NSObject:AnyObject],上下文:UnsafeMutablePointer){
如果上下文==&myContext{
println(“字符更新:\(更改[NSKeyValueChangeNewKey]))
}否则{
super.observeValueForKeyPath(键路径,of对象:object,change:change,context:context)
}
}
脱硝{
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设置为其弱属性

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

protocol UpdateTapLabel {
func didTapSomeOperateAction(operateInfo : String)
}

}

}

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]
    }
}
//您应该像这样添加观察者
//addObserver(self,forKeyPath:“childView.char”,选项:NSKeyValueObservingOptions.New,上下文:nil)
//您还应该在Denit中删除它,否则在释放parentView时会崩溃
脱硝{
self.removeObserver(self,forKeyPath:“childView.char”)
}
//您可以在这里处理kvo
重写func observeValueForKeyPath(键路径:字符串,对象对象:AnyObject,更改:[NSObject:AnyObject],上下文:UnsafeMutablePointer){
如果keyPath==“childView.char”{
self.tapLabel.text=更改[NSKeyValueChangeNewKey]
}
}

非常感谢!那么,我现在如何根据子视图的结果更改父视图中的标签呢?到目前为止,我有这个,不幸的是,它抛出了一个零的错误
ParentView().tapLabel.text=getChar()
您的项目是关于计算器的?两种方式,类似的。这是一个教孩子们如何编码的简单应用程序。当我试图让我的类继承NSObject时,我遇到了一个错误,因为它已经是一个UIViewController,我应该创建一个新类吗?哦,不,我只是以NSObject为例,你应该让你的类继承Hickey你应该让你的类继承Uiview或Uiviecontroller或其他一些
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)
}
//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]
    }
}