gpt4 book ai didi

ios - 为什么我不能在我的prepareForSegue方法中快速转换发送者?

转载 作者:行者123 更新时间:2023-11-30 13:35:18 24 4
gpt4 key购买 nike

我的应用程序中有一个 ListView ,我正在动态添加单元格。对于每个单元格,我想附加一个segue,并感谢它在用户按下单元格时打开一个UIViewController(称为FullUser)(该UIViewController将包含此segue传递的数据,从所选的特定单元格获取)由用户存储在名为 SingleUser 的结构中)。所以我有这个方法:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath) as! SingleEventCell

let user = self.items[indexPath.row] as? SingleUser

self.performSegueWithIdentifier("fullUserFromListSegue", sender: user)
}

然后:

var fullUserDetails: FullUser?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "fullUserFromListSegue"){

fullUserDetails = segue.destinationViewController as? FullUser

//now I want to set the textfield on the other panel:

let user2 = sender as? SingleUser

var username = user2?.username

fullRequestDetails!.username.text = username!
}
}

但是我收到错误,当我在调试器中检查它时,我发现 user2 为零。为什么这样?我想在那里存储一个 SingleUser 对象,那么如何将它传递到那里呢?

最佳答案

将逻辑移至 prepareForSegue() 函数中,然后在目标 View Controller 上设置属性。请注意,如果 destinationViewController 不属于您期望的类,那么您应该检查 Storyboard中的 View Controller 设置。

var fullUserDetails: FullUser?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// check the destintation view controller is of correct class
if let fullUserDetails = segue.destinationViewController as? FullUser {
let path = self.tableView.indexPathForSelectedRow
let user = self.items[path.row] as? SingleUser

var username = user?.username

fullRequestDetails!.username.text = username!
}
}

关于ios - 为什么我不能在我的prepareForSegue方法中快速转换发送者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36141563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com