gpt4 book ai didi

ios - 如何将文本放入 segue 函数以传递给下一个 Controller ?

转载 作者:搜寻专家 更新时间:2023-11-01 07:11:24 25 4
gpt4 key购买 nike

我想获取标题并将其作为 titleText 放入作为标签的 DestVC 中。如何将它放入 segue 函数中?

import UIKit
import Firebase
import FirebaseDatabase
import SDWebImage


struct postStruct {
let title : String!
let author : String!
let date : String!
let article : String!
let downloadURL : String!

}

class ZeroHomeViewController: UITableViewController {




var posts = [postStruct]()
var downloadURL : String = ""






override func viewDidLoad() {
super.viewDidLoad()


let ref = Database.database().reference().child("Posts")

ref.observeSingleEvent(of: .value, with: { snapshot in

print(snapshot.childrenCount)

for rest in snapshot.children.allObjects as! [DataSnapshot] {

guard let value = rest.value as? Dictionary<String,Any> else { continue }

guard let title = value["Title"] as? String else { continue }
guard let downloadURL = value["Download URL"] as? String else { continue }
guard let author = value["Author"] as? String else { continue }
guard let date = value["Date"] as? String else { continue }
guard let article = value["Article"] as? String else { continue }


let post = postStruct(title: title, author: author, date: date, article: article, downloadURL: downloadURL)

self.posts.append(post)


}

self.posts = self.posts.reversed(); self.tableView.reloadData()

})


}




override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

let label1 = cell?.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].title


let imageView = cell?.viewWithTag(2) as! UIImageView
let post = self.posts[indexPath.row];
imageView.sd_setImage(with: URL(string: post.downloadURL), placeholderImage: UIImage(named: "placeholder"))

return cell!

}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detail" {
if let indexPath = tableView.indexPathForSelectedRow {
let destVC = segue.destination as! ArticleViewController
destVC.titleText = value["Title"] as? String

}

}
}

最佳答案

您只需从您的posts 数组访问相关的postStruct,然后获取标题。您已经有了所选行的索引路径; .row 属性将成为您所需结构的 posts 数组中的索引。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detail" {
if let indexPath = tableView.indexPathForSelectedRow {
let destVC = segue.destination as! ArticleViewController
destVC.titleText = posts[indexPath.row].title

}

}
}

关于ios - 如何将文本放入 segue 函数以传递给下一个 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44710213/

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