ios - 回复按钮导致崩溃

标签 ios swift xcode

我正在制作一个 4Chanesque 回复 View Controller 。我的计划是允许用户在 textView 中写回复。然后回复按钮应该获取他们的文本并将其添加到 Post 类中称为“comment”的字符串数组中。然后 tableView 将在该帖子的评论部分底部显示他们的评论。

这是我的评论 View Controller :

import Foundation
import UIKit
import Firebase

class MainTextView: UIViewController, UITextViewDelegate {

@IBOutlet weak var titleText: UILabel!
@IBOutlet weak var mainText: UILabel!

@IBOutlet weak var commentPlaceHolder: UILabel!
@IBOutlet weak var newCommentLabel: UITextView!
weak var delegate:NewPostVCDelegate?

@IBAction func reply(_ sender: UIButton) {

// Firebase code here
    let postRef = Database.database().reference().child("posts").childByAutoId()

    let postObject = [
        "comment": newCommentLabel.text,
        "timestamp": [".sv": "timestamp"]
        ] as [String : Any]

    postRef.setValue(postObject, withCompletionBlock: { error, ref in
        if error == nil {
            self.delegate!.didUploadPost(withID: ref.key!)
            self.dismiss(animated: true, completion: nil)
        }  else {
            // Handle error

        }
    })
    newCommentLabel.text = String()
    commentPlaceHolder.isHidden = false
}
var post: Post?

作为引用,这是我的 Post 类

import Foundation

class Post {
var id:String
var title: String
var text:String
var createdAt:Date
var comment: [String] = []

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




}
static func parse(_ key:String, data:[String:Any]) -> Post? {
    if let title = data["text"] as? String,
        let text = data["title"] as? String,
        let timestamp = data["timestamp"] as? Double {
        return Post(id: key, title: title,  text: text, timestamp:timestamp, comment: [])
        }


    return nil
    }
}

NewPostVCDelegate 在我的 NewPostViewController 中这样声明:

protocol NewPostVCDelegate: class {
func didUploadPost(withID id:String)
  }

我想知道当我真的只想在现有帖子中添加评论时,委托(delegate)是否让我尝试发表新帖子。我不确定如何以编程方式纠正此问题。目前,当我按下回复按钮时,我收到一条崩溃消息。它显示:“线程 1: fatal error :在展开可选值时意外发现 nil”。此错误消息红色突出显示行:“self.delegate!.didUploadPost(withID: ref.key!)”。感谢您的帮助。

最佳答案

既然崩溃就在这里

self.delegate!.didUploadPost(withID: ref.key!)

你的委托(delegate)是nil,你需要在这里设置它destination.delegate = self

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

关于ios - 回复按钮导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56628629/

相关文章:

ios - 在 UIScrollView 中嵌入 UIStackView 时出现问题

SWIFT:转换格式为 E、dd、MMM、y、KK:mma 的日期

objective-c - Nib 中的原型(prototype)单元而不是 Storyboard

ios - 如何在第二个 View Controller 中初始化 View Controller socket ?

ios - 向下滚动时,UICollectionView 中的图像会自动翻转吗?

ios - 禁用自动隐藏字幕 ios Storyboard

ios - 如何在第一次点击后停止重新生成墙?

swift - 观看按钮按下崩溃

arrays - 我想将具有属性(btnImage + btnTitle)的字典数组从Plist读取到TableViewCell中

objective-c - 如何查找 NSTimer 是否处于事件状态?