繁体   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?

在我的应用程序中,我在容器内有一个嵌入式UIViewController。 在我的故事板上,我向此fullRequestSegue添加了一个触摸事件,并将其称为segue: fullRequestSegue

然后,在该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      
    }
}
}

然后在我的类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

}

这里有什么问题?

问题是当方法changeEventDescr被调用时, userNumber标签未初始化。 您正在尝试分配给一个nil对象。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM