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.
我遇到了与这个问题相同的错误:如何在另一个类中访问IBOutlet ?在swift中,但是当我在我的Xcode(针对iOS 8和swift 3)中写入时,我有一个错误。
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:
我的代码是这样的。我想编辑的是Convert_Table_Third_Cell类中的amauntOut(是一个UILabel),它的作用是一个按钮:
@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?
实例成员'instantiateViewController'不能在‘UIStoryboard’类型上使用;你是想用这个类型的值来代替吗?
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
进口UIKit
class Convert_Table_Third_Cell: UITableViewCell {
类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 个解决方案
#1
0
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.
你的方法是不正确的。视图控制器在屏幕上显示时被启动。一次只能显示一个视图控制器对象。在代码中,您正在初始化一个全新的视图控制器,并将文本设置为outlet。这是行不通的。相反,您需要将文本设置为现有的视图控制器实例的文本字段。
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)
#2
1
Instance member 'instantiateViewController' cannot be used on type 'UIStoryboard'; did you mean to use a value of this type instead?
实例成员'instantiateViewController'不能在‘UIStoryboard’类型上使用;你是想用这个类型的值来代替吗?
It say that you cannot use instance member
of instantiateViewController
by a class
of UIStoryboard
.
它说你不能使用uistoryboardviewcontroller的实例成员。
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
更改var update: Convert_Table_Third_Cell = UIStoryboard.instantiateViewController(uistoryboardviewcontroller)为!Convert_Table_Third_Cell到var update: Convert_Table_Third_Cell = storyboard?instantiateViewController(withIdentifier:{ YourStoryBoardID })!Convert_Table_Third_Cell
#1
0
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.
你的方法是不正确的。视图控制器在屏幕上显示时被启动。一次只能显示一个视图控制器对象。在代码中,您正在初始化一个全新的视图控制器,并将文本设置为outlet。这是行不通的。相反,您需要将文本设置为现有的视图控制器实例的文本字段。
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)
#2
1
Instance member 'instantiateViewController' cannot be used on type 'UIStoryboard'; did you mean to use a value of this type instead?
实例成员'instantiateViewController'不能在‘UIStoryboard’类型上使用;你是想用这个类型的值来代替吗?
It say that you cannot use instance member
of instantiateViewController
by a class
of UIStoryboard
.
它说你不能使用uistoryboardviewcontroller的实例成员。
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
更改var update: Convert_Table_Third_Cell = UIStoryboard.instantiateViewController(uistoryboardviewcontroller)为!Convert_Table_Third_Cell到var update: Convert_Table_Third_Cell = storyboard?instantiateViewController(withIdentifier:{ YourStoryBoardID })!Convert_Table_Third_Cell