[英]Why I cannot set the labels with the data passed from one UIViewController to another (printing the data in console works) in Swift?
In my app I have an embedded UIViewController inside a container. 在我的应用程序中,我在容器内有一个嵌入式UIViewController。 In my story board I added a touch event to this viewcontroller and I called the segue:
fullRequestSegue
在我的故事板上,我向此
fullRequestSegue
添加了一个触摸事件,并将其称为segue: fullRequestSegue
Then in the code of that UIViewController I wrote: 然后,在该UIViewController的代码中,我写了:
class RequestDetails: UIViewController, ShowRequestDetailsFromMap {
var fullRequestDetails: FullRequestFromMap?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "fullRequestSegue"){
fullRequestDetails?.changeEventDescr(self.descr, username: "adam", number: "12", created_at: self.created_at, photo: self.photo)
fullRequestDetails = segue.destinationViewController as? FullRequestFromMap
fullRequestDetails!.showRequestDetails = self
}
}
}
Then in my class FullRequestFromMap
I have: 然后在我的类
FullRequestFromMap
我有:
protocol ShowRequestDetailsFromMap {
func changeEventDescr(text:String)
}
class FullRequestFromMap: UIViewController{
@IBOutlet weak var userNumber: UILabel!
var showRequestDetails:ShowRequestDetailsFromMap?
override func viewDidLoad() {
super.viewDidLoad()
}
func changeEventDescr(description: String, username: String, number: String, created_at: NSDate, photo: String) {
print(username)
print(description)
print(number)
print(created_at)
print(photo) //that works fine, I see all valid data in the console
userNumber.text = number //doesn't work, I see empty label instead of filled with passed data, the same problem is with other labels
}
What is the problem here? 这里有什么问题?
The problem is when the method changeEventDescr
is called the userNumber
label is not initialized. 问题是当方法
changeEventDescr
被调用时, userNumber
标签未初始化。 You are trying to assign to a nil object. 您正在尝试分配给一个nil对象。
Create a string variable in your FullRequestFromMap
class and store text in it and in your viewDidLoad
method you have to assign the text to userNumber
label. 在
FullRequestFromMap
类中创建一个字符串变量,然后在其中存储文本,然后在viewDidLoad
方法中将文本分配给userNumber
标签。
class FullRequestFromMap: UIViewController {
@IBOutlet weak var userNumber: UILabel!
var showRequestDetails:ShowRequestDetailsFromMap?
var userNumberLabelText:String = "Default Value"
override func viewDidLoad() {
super.viewDidLoad()
userNumber.text = userNumberLabelText
}
func changeEventDescr(description: String, username: String, number: String, created_at: NSDate, photo: String) {
print(username)
print(description)
print(number)
print(created_at)
print(photo)
userNumberLabelText = number // Here you set the class variable, not the label it self
}
}
class RequestDetails: UIViewController {
......
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "fullRequestSegue") {
fullRequestDetails = segue.destinationViewController as? FullRequestFromMap
// Option 1: You can directly assign it
fullRequestDetails?.userNumberLabelText = "12"
// Option 2: You can call your method
fullRequestDetails?.changeEventDescr(self.descr, username: "adam", number: "12", created_at: self.created_at, photo: self.photo)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.