如何在另一个 class 中访问 IBOutlet?

How can I access IBOutlet in another class?

我在这个问题中遇到了同样的错误:how can i access IBOutlet in another class? in swift 但是当我在 Xcode 中写入时(对于 iOS 8 和 Swift 3)我有错误。

我的代码是这样的。我想编辑 amauntOut(是一个 UILabel),它位于 class Convert_Table_Third_Cell 中,只需按一下按钮即可:

@IBAction func actionTextb(_ sender: Any) {
    print("you writted \(String(describing: amauntEnter.text!))----")

    //Convert_Table_Third_Cell.amauntOut.text = amauntEnter.text ----> is a tried
    //var dash : abcViewController = storyBoard.instantiateViewControllerWithIdentifier("abc") as! abcViewController ----> is a tried
    //var a = dash.getXYZ() ----> is a tried

    var update: Convert_Table_Third_Cell = UIStoryboard.instantiateViewController(UIStoryboard) as! Convert_Table_Third_Cell
    update.amauntOut.text = "hola"
}

我收到这个错误:

Instance member 'instantiateViewController' cannot be used on type 'UIStoryboard'; did you mean to use a value of this type instead?

有人可以帮助我吗?

这是第一个class

import UIKit

class Convert_Table_Second_Cell: UITableViewCell {

    @IBOutlet var amauntEnter: UITextField!

    var theNumber = getTheNumber()



    @IBAction func actionTextb(_ sender: Any) {
        print("you writted \(String(describing: amauntEnter.text!))----")

        let storyboard = UIStoryboard.init(name: "convert ID", bundle: nil)
        let update = storyboard.instantiateViewController(withIdentifier:  "convert ID") as!  Convert_Table_Third_Cell

        update.amauntOut.text = "hola"


        let aa = "hola hillel----------------------"
        print(aa)
        print(theNumber.usedParameters(ArrayOfNumbers: unitInOutlet, TipOfData: 3))

    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        print("this is the valeu \(theNumber.hola)")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

这是第二个我要编辑的标签在哪里

导入 UIKit

class Convert_Table_Third_Cell: UITableViewCell {

@IBOutlet var amauntOut: UILabel!

@IBOutlet var UnityMeasurment: UILabel!


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

您的方法不正确。视图控制器在屏幕上显示时启动。一次只能显示一个视图控制器对象。在您的代码中,您正在启动一个全新的视图控制器并将文本设置为插座。所以那是行不通的。相反,您需要将文本设置为视图控制器现有实例上的文本字段。

为此,在您要接收文本字段内容更新的视图控制器中,在通知中心注册以接收内容更新功能调用。

NotificationCenter.default.addObserver(self, selector: #selector(listnerFunction(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)

func listnerFunction(_ notification: NSNotification) {
    if let data = notification.userInfo?["data"] as? String {
        self.textField.text = data
    }
}

然后在另一个视图控制器中,如果你想发送文本到上面的视图控制器并更新文本,只需post数据到通知中心

let data:[String: String] = ["data": "YourData"]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: data) 

Instance member 'instantiateViewController' cannot be used on type 'UIStoryboard'; did you mean to use a value of this type instead?

它说你不能通过 UIStoryboardclass 使用 instantiateViewControllerinstance member

var update: Convert_Table_Third_Cell = UIStoryboard.instantiateViewController(UIStoryboard) as! Convert_Table_Third_Cell 更改为var update: Convert_Table_Third_Cell = storyboard?.instantiateViewController(withIdentifier: {YourStoryBoardID}) as! Convert_Table_Third_Cell