gpt4 book ai didi

ios - 调用函数为类成员设置标签

转载 作者:行者123 更新时间:2023-11-29 05:41:34 26 4
gpt4 key购买 nike

我在 UITableView 单元格 中有一组帖子。我创建了一个函数,以便当您单击单元格时,您会转到一个新的UIViewController。现在我希望新的 UIViewController 根据您单击的帖子更新正文和标题。我有新 UIViewController 的以下文本:

import UIKit
import Firebase
class MainTextView: UIViewController {
@IBOutlet weak var titleText: UILabel!
@IBOutlet weak var mainText: UILabel!

func setMain(post:Post){
titleText.text = post.text
mainText.text = post.title
}
}

以及我的主视图 Controller 的以下文本:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
let row = indexPath.row
print("Row: \(row)")
let destination = storyboard!.instantiateViewController(withIdentifier: "MainTextView") as! MainTextView
navigationController?.pushViewController(destination, animated: true)
destination.setMain(post: posts[indexPath.row])
}

}

这是我的帖子类(class):

导入基金会

类帖子{ 变量 ID:字符串 var 标题:字符串 var 文本:字符串 var 创建时间:日期

init(id: String, title: String,text:String,  timestamp:Double) {
self.id = id
self.title = title
self.text = text
self.createdAt = Date(timeIntervalSince1970: timestamp / 1000)

}

}

我应该注意到,我在 HomeViewController 函数 fetchPost 中也收到以下错误:

 func fetchPosts(completion:@escaping (_ posts:[Post])->()) {
let postsRef = Database.database().reference().child("posts")
var queryRef:DatabaseQuery
let lastPost = posts.last
if lastPost != nil {
let lastTimestamp = lastPost!.createdAt.timeIntervalSince1970 * 1000
queryRef = postsRef.queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp).queryLimited(toLast: 20)
} else {
queryRef = postsRef.queryOrdered(byChild: "timestamp").queryLimited(toLast: 20)
}

queryRef.observeSingleEvent(of: .value, with: { snapshot in
var tempPosts = [Post]()

for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String:Any],
let title = dict["text"] as? String,

let text = dict["title"] as? String,
let timestamp = dict["timestamp"] as? Double {

if childSnapshot.key != lastPost?.id {
let post = Post(id: childSnapshot.key, title: title, text: text, timestamp:timestamp)
tempPosts.insert(post, at: 0)
}

}
}

return completion(tempPosts)
})
}

这是我的 fetchPosts 函数:

func fetchPosts(completion: @escaping( _ posts:[Post])->()) {

let postsRef = Database.database().reference().child("posts")
let lastPost = self.posts.last
var queryRef:DatabaseQuery

if lastPost == nil {
queryRef = postsRef.queryOrdered(byChild: "timestamp").queryLimited(toLast: 10)
} else{
let lastTimestamp = lastPost!.createdAt.timeIntervalSince1970 * 1000
queryRef = postsRef.queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp)
}


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

var tempPosts = [Post]()

for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String:Any],
let text = dict["text"] as? String,
let comment = dict["comment"] as? Array<String>,
let title = dict["title"] as? String,
let timestamp = dict["timestamp"] as? Double{
if childSnapshot.key != lastPost?.id {
let post = Post(id: childSnapshot.key, title: text, text: title, comment: comment, timestamp: timestamp)
tempPosts.insert(post, at: 0)
}

}
}
return completion(tempPosts)

})



}

最佳答案

更改 MainTextView Controller 的逻辑:

import UIKit
import Firebase

class MainTextView: UIViewController {

// MARK: - Your properties

@IBOutlet weak var titleText: UILabel!

@IBOutlet weak var mainText: UILabel!

var post: Post?

// MARK: - View Controller LifeCycle

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.setMain()
}

private func setMain() {
guard let post = self.post else {
return
}

titleText.text = post.text
mainText.text = post.title
}
}

您还应该更改 Controller 的逻辑

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)

if let destination = storyboard?.instantiateViewController(withIdentifier:"MainTextView") as? MainTextView {
destination.post = <list of post>[indexPath.row]
navigationController?.pushViewController(destination, animated: true)
}
}

如果您在项目中使用 Segues,您可以尝试以下决定:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
performSegue(withIdentifier: "Your segue name", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Your segue name", let destination = segue.destination as? MainTextView {
destination.post = <Your selected post>
}
}

关于ios - 调用函数为类成员设置标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56500355/

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