Ios Segue未将值从my StoriesTableViewController传递到SingleArtCallViewController。为什么会这样?
如果用户单击我的StoriesTableViewController中的表项,我需要将id值传递给SingleArtCallViewController,以便SingleArtCallViewController可以显示适当的详细信息 问题是,如果我打印用户按下的项目的id,它就正确地打印了它。但是它不会在另一个ViewController中接收。有人能帮我解决这个问题吗 StoriesTableViewController:Ios Segue未将值从my StoriesTableViewController传递到SingleArtCallViewController。为什么会这样?,ios,swift,segue,Ios,Swift,Segue,如果用户单击我的StoriesTableViewController中的表项,我需要将id值传递给SingleArtCallViewController,以便SingleArtCallViewController可以显示适当的详细信息 问题是,如果我打印用户按下的项目的id,它就正确地打印了它。但是它不会在另一个ViewController中接收。有人能帮我解决这个问题吗 StoriesTableViewController: override func tableView(_ tableVie
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
finalId = idTable[myIndex]
let story = stories[indexPath.row]
ArtcallID = story.id
performSegue(withIdentifier: "singleArtcall", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print(ArtcallID + "is the index of the item that was selected")
//this correctly prints the id
let id = segue.destination as! SingleArtcallViewController
id.ArtcallID = ArtcallID
// print("id: ",id)
}
var ArtcallID: String!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("ArtcallID: ", ArtcallID)
//If I print this I"m getting the result "ArtcallID: some("")"
}
SingleArtCallViewController:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
finalId = idTable[myIndex]
let story = stories[indexPath.row]
ArtcallID = story.id
performSegue(withIdentifier: "singleArtcall", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print(ArtcallID + "is the index of the item that was selected")
//this correctly prints the id
let id = segue.destination as! SingleArtcallViewController
id.ArtcallID = ArtcallID
// print("id: ",id)
}
var ArtcallID: String!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("ArtcallID: ", ArtcallID)
//If I print this I"m getting the result "ArtcallID: some("")"
}
Story.swift:
import Firebase
class Story
{
var id: String = ""
var text: String = ""
var numberOfLikes = 0
var numberOfAngry = 0
let ref: DatabaseReference!
var fundedAmt: Int = 0
var targetAmt: Int = 0
var perfCity: String = ""
var targetDate: IntMax = 0
var backers: Int = 0
var fundedPercent: Int = 0
var imageURL: String = ""
init(text: String) {
self.text = text
ref = Database.database().reference().child("Fund").childByAutoId()
}
init(snapshot: DataSnapshot)
{
ref = snapshot.ref
if let value = snapshot.value as? [String : Any] {
text = value["artist_name"] as! String
backers = value["total_backers"] as! Int
perfCity = value["perf_location"] as! String
fundedAmt = value["amount_funded"] as! Int
targetAmt = value["target_amount"] as! Int
fundedPercent = value["percent_funded"] as! Int
// numberOfLikes = value["numberOfLikes"] as! Int
// numberOfAngry = value["numberOfAngry"] as! Int
id = snapshot.key
}
}
可以肯定的是,您的:story.id是字符串吗 您是否尝试通过另一个测试值?如果是这样的话,它有效吗 试试这个:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "singleArtcall"{
let id = segue.destination as! SingleArtCallViewController
id.getID = ArtcallID
}
}
如果我正确理解您试图实现的目标,并且您通过将控件从原型单元格拖动到SingleArtcallViewController来创建segue:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
finalId = idTable[myIndex]
let story = stories[indexPath.row]
ArtcallID = story.id
performSegue(withIdentifier: "singleArtcall", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print(ArtcallID + "is the index of the item that was selected")
//this correctly prints the id
let id = segue.destination as! SingleArtcallViewController
id.ArtcallID = ArtcallID
// print("id: ",id)
}
var ArtcallID: String!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("ArtcallID: ", ArtcallID)
//If I print this I"m getting the result "ArtcallID: some("")"
}
代码不起作用的具体原因是为segue做准备:。。。在tableView之前运行\uTableView:UITableView,在indexPath:indexPath上运行。跑得越快,准备赛格两次
如果是这样,你就不需要这个方法了
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
只需使用preparefor segue:…:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let selectedRow = tableView.indexPathForSelectedRow?.row,
let vc = segue.destination as? SingleArtcallViewController else {return}
vc.artCallId = stories[selectedRow].id
}
SingleArtCallViewController或SingleArtCallViewController哪一个是对的?SingleArtCallViewController是对的。但要回答您的问题,如果我传递字符串“hello”,就会被传输!让我上传我的Story.swift文件。尝试在if条件后的prepare中添加一个print,它正在打印什么?因此我在if之后添加了以下print语句:printgetID+是getID的值。ARtcallID的值是:+ARtcallID My output:是getID的值。ARtcallID的值为:-asdffdsa123,这意味着ARtcallID被正确记录,但字符串getID未采用其值。我不知道为什么。像做梦一样工作!非常感谢!