Hillel Picciotto
Hillel Picciotto

Reputation: 37

How can I access IBOutlet in another class?

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.

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

I get this error:

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

Can someone help me?

this is the first 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
    }

}

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

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

}

Upvotes: 2

Views: 1416

Answers (2)

Codus
Codus

Reputation: 1473

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

It say that you cannot use instance member of instantiateViewController by a class of UIStoryboard.

Change var update: Convert_Table_Third_Cell = UIStoryboard.instantiateViewController(UIStoryboard) as! Convert_Table_Third_Cell to var update: Convert_Table_Third_Cell = storyboard?.instantiateViewController(withIdentifier: {YourStoryBoardID}) as! Convert_Table_Third_Cell

Upvotes: 1

Fangming
Fangming

Reputation: 25260

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) 

Upvotes: 1

Related Questions