简体   繁体   English

为什么我不能在Swift中使用从一个UIViewController传递到另一个UIViewController的数据来设置标签(在控制台中打印数据)?

[英]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.

相关问题 将数据从一个UIViewController委派给另一个UIViewController - Delegate data from one UIViewController to another one 如何通过UITapGestureRecognizer将数据从一个UIViewController传递到另一个UIViewController - How to pass data from one UIViewController to another one through UITapGestureRecognizer 从 UIViewController 获取数据到另一个 UIViewController - Get Data from UIViewController to Another UIViewController 在Swift中以编程方式从一个UIViewController移至另一个 - Moving from one UIViewController to another in Swift programmatically 我如何解析来自 UIViewController 的数据,该数据具有 2 个 CollectionViews 和 swift - How can I parse data from a UIViewController having 2 CollectionViews with swift 如何在Swift中将数据从UITableViewRowAction传递到UIViewController? - How to pass data to a UIViewController from a UITableViewRowAction in Swift? 将数据从tableView传递到UIViewController swift 3 - Passing data from tableView to UIViewController swift 3 在 Swift 5 中将数据从 UIPickerView 传递到 UIViewController - Passing data from a UIPickerView to a UIViewController in Swift 5 Swift:将数据从UITableView发送到UIViewController - Swift: Send data from a UITableView to UIViewController 从数据快速“生成”标签 - "Generate" labels from data swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM