【问题标题】:Pass variable from child view to parent view in Swift在 Swift 中将变量从子视图传递到父视图
【发布时间】:2015-11-12 21:00:56
【问题描述】:

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

}

}

【问题讨论】:

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


    【解决方案1】:

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

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

    你也可以使用KVO来观察子view的char变量;或者你也可以使用NSNotification来更新父view的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]
        }
    }
    

    【讨论】:

    • 当我尝试让我的类继承 NSObject 时出现错误,因为它已经是 UIViewController,我应该创建一个新类吗?
    • 哦不,我只是以NSObject为例,你应该问你的类继承hickey
    • 你应该要求你的类继承 Uiview 或 Uiviecontroller 或其他一些
    【解决方案2】:

    好的,也许你可以将子视图设置为父视图的属性,也许是这样的

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

    然后你就可以这样使用了

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

    【讨论】:

    • 非常感谢!那么我现在如何根据子视图的结果更改父视图中的标签?到目前为止,我有这个,不幸的是它抛出了一个 nil 错误。 ParentView().tapLabel.text = getChar()
    • 您的项目是关于计算器的?两种方式,
    • 类似的东西。这是一个教孩子如何编程的简单应用。
    猜你喜欢
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    相关资源
    最近更新 更多