我如何在另一个类中访问 IBOutlet? [英] How can I access IBOutlet in another class?

查看:26
本文介绍了我如何在另一个类中访问 IBOutlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个问题中遇到了同样的错误:我如何在另一个类中访问 IBOutlet?在 swift 但是当我在我的 Xcode 中(对于 iOS 8 和 Swift 3)我有一个错误.

I am having the same error that is in this question: how can i access IBOutlet in another class? in swift but when I write in my Xcode (for iOS 8 with Swift 3) I have an error.

我的代码是这样的.我想通过一个按钮的动作编辑 amauntOut(是一个 UILabel),它位于 Convert_Table_Third_Cell 类中:

My code is this. I want to edit amauntOut (is an UILabel) that is in the class Convert_Table_Third_Cell with the action of one button:

@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';您是想改用这种类型的值吗?

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

有人可以帮我吗?

这是第一堂课

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
    }

}

这是我要编辑的标签的第二个位置

this is the second where is the label that I want to edit

导入 UIKit

class Convert_Table_Third_Cell: UITableViewCell {

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
}

}

推荐答案

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

Your approach is incorrect. A view controller is initiated when it is displayed on the screen. One and only on view controller object can be displayed at one time. In your code, you are initiating a brand new view controller and set text to outlets. So that won't work. Instead, you need to set text to the text field on the existing instance of you view controller.

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

To do so, in the view controller that you want to receive text field content updates, register in notification center to receive a content update function calls.

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

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

Then in another view controller, if you want to send text to the above view controller and update text, simply post the data to notification center

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

这篇关于我如何在另一个类中访问 IBOutlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆