调用函数为类成员设置标签
Posted
技术标签:
【中文标题】调用函数为类成员设置标签【英文标题】:Calling a Function to Set a Label for a Member of a Class 【发布时间】:2019-06-07 19:39:43 【问题描述】:我在UITableView cells
中有一系列帖子。我做了一个功能,当你点击cell
时,你会转到一个新的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
我的主视图控制器的以下文本:
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])
这是我的帖子类:
导入基础
类帖子 变量 ID:字符串 变量标题:字符串 可变文本:字符串 var createdAt:Date
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)
)
【问题讨论】:
destination.setMain(post: somePost)
.
我用destination.setMain(post:Post)替换了setMain(post:Post)。我收到错误:无法将类型“Post.Type”的值转换为预期的参数类型“Post”。我对你在这里的意思有点困惑。我也尝试了 somePost 并出现错误...谢谢您的帮助
我放了somePost
,因为你需要传递Post
对象的一些实例。
看来你是对的!我需要了解实例。这看起来是一个好的开始:***.com/questions/45989491/…
【参考方案1】:
更改 MainTextView 控制器的逻辑:
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
你也应该改变控制器的逻辑
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>
【讨论】:
当你写通过
destination.setMain(post: Post)
因为函数位于目标类而不是HomeViewController
【讨论】:
以上是关于调用函数为类成员设置标签的主要内容,如果未能解决你的问题,请参考以下文章