簡體   English   中英

如何將情節提要鏈接到視圖控制器

[英]How to link storyboard to view controller

我正在嘗試將我制作的情節提要板連接到我的聊天應用程序中的視圖控制器文件。

我所做的是...

1)創建一個視圖控制器文件(swift)。

2)創建一個故事板文件,並將視圖控制器類設置為自定義類。

3)將故事板中的組件(表視圖)連接到視圖控制器作為插座。

現在,我收到“線程1:致命錯誤:在展開可選值時意外發現nil”錯誤消息。

代碼如下。

import UIKit

class ChatViewController: UIViewController {

    var _device_width: Int = 0
    var _device_height: Int = 0
    var _footer_size: Int = 0
    var _backBtn: UIBarButtonItem!
    var window: UIWindow?
    var _toolBar: UIToolbar!
    var bottomView: ChatRoomInputView!
    var chats: [ChatEntity] = []

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        showNavigateBar()

        setupUI()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }

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

    override var canBecomeFirstResponder: Bool {
        return true
    }

    override var inputAccessoryView: UIView? {
        return bottomView
    }


    func showNavigateBar() {
        let backButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        navigationItem.backBarButtonItem = backButtonItem
        self.title = 'My App'

    }
}

extension ChatViewController {
    func setupUI() {
        self.view.backgroundColor = UIColor(red: 113/255, green: 148/255, blue: 194/255, alpha: 1)

        // ERROR happen on this line.
        // "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"
        tableView.separatorColor = UIColor.clear


        tableView.estimatedRowHeight = 10000
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.allowsSelection = false
        tableView.keyboardDismissMode = .interactive

        tableView.register(UINib(nibName: "YourChatViewCell", bundle: nil), forCellReuseIdentifier: "YourChat")
        tableView.register(UINib(nibName: "MyChatViewCell", bundle: nil), forCellReuseIdentifier: "MyChat")

        let chat1 = ChatEntity(text: "text1", time: "10:01", userType: .I)
        let chat2 = ChatEntity(text: "text2", time: "10:02", userType: .You)
        let chat3 = ChatEntity(text: "text3", time: "10:03", userType: .I)
        chats = [chat1, chat2, chat3]
    }
}

extension ChatViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let chat = self.chats[indexPath.row]
        if chat.isMyChat() {
            let cell = tableView.dequeueReusableCell(withIdentifier: "MyChat") as! MyChatViewCell
            cell.clipsToBounds = true
            // Todo: isRead
            cell.updateCell(text: chat.text, time: chat.time, isRead: true)
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "YourChat") as! YourChatViewCell
            cell.clipsToBounds = true
            cell.updateCell(text: chat.text, time: chat.time)
            return cell
        }
    }
}

extension ChatViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath)
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 10
    }
}

我將視圖控制器定義為自定義類。 在此處輸入圖片說明

我該怎么做才能正確工作? 請給我一個建議。

加)

ChatViewController由另一個類文件調用。

let chatViewController = ChatViewController()
self.navigationController?.pushViewController(chatViewController, animated: false)

而且我相信@IBOutlet連接正確。

ChatViewController.swift

在此處輸入圖片說明

ChatViewController.storyboard

在此處輸入圖片說明

而且我知道調用此函數時tableView為nil。

在此處輸入圖片說明 實際上,我對分鏡腳本的了解不多,因為我通常不使用分鏡腳本。 我更喜歡通過代碼創建UI。 但是現在,我必須使用情節提要文件,因為我想使用聊天系統的示例項目。

ADD2)

我認為我可能會誤判情節提要的設置。 我不知道如何在情節提要上使用導航控制器或入口點。

在此處輸入圖片說明

您應該在viewLoad()函數中添加委托和數據源。

 override func viewDidLoad() {
    super.viewDidLoad()

    showNavigateBar()
    tableView.delegate = self
    tableView.dataSource = self
    setupUI()
}

如果在那之后錯誤仍然存​​在,請檢查情節提要中的“插座”。

我解決了這個問題。

我修改了調用方法。

let storyBoard : UIStoryboard = UIStoryboard(name: "ChatViewController", bundle:nil)
let chatViewController = storyBoard.instantiateViewController(withIdentifier: "ChatViewController") 
self.navigationController?.pushViewController(chatViewController, animated: true)

謝謝您的好意建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM