如何在另一个班级访问IBOutlet?

时间:2017-07-14 18:22:51

标签: ios swift swift3 iboutlet

我遇到了与此问题相同的错误:how can i access IBOutlet in another class? in swift但是当我在Xcode中编写(对于带有Swift 3的iOS 8)时,我遇到了错误。

我的代码是这样的。我想使用一个按钮的操作来修改amauntOut类中的UILabelConvert_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"
}

我收到此错误:

  

实例成员' instantiateViewController'不能用于类型' UIStoryboard&#39 ;;你的意思是使用这种类型的值吗?

有人可以帮助我吗?

这是第一堂课

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
}

}

2 个答案:

答案 0 :(得分:1)

  

实例成员' instantiateViewController'不能用于类型' UIStoryboard&#39 ;;你的意思是使用这种类型的值吗?

它表示您不能instance member instantiateViewController class使用UIStoryboard {/ 1}}。

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

答案 1 :(得分:0)

您的方法不正确。视图控制器在屏幕上显示时启动。可以一次显示一个且仅在视图控制器对象上。在您的代码中,您将启动一个全新的视图控制器并将文本设置为出口。所以这不会起作用。相反,您需要将文本设置为视图控制器的现有实例上的文本字段。

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

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

然后在另一个视图控制器中,如果要将文本发送到上述视图控制器并更新文本,只需将数据发布到通知中心

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