如何将故事板链接到视图控制器

时间:2018-04-22 07:26:54

标签: ios swift uistoryboard

我正在尝试将我制作的故事板连接到我的聊天应用程序中的视图控制器文件。

我做的是......

1)创建一个视图控制器文件(swift)。

2)创建一个storyboard文件并将视图控制器类设置为自定义类。

3)将我的故事板中的组件(表格视图)连接到视图控制器作为插座。

现在,我得到了#34;线程1:致命错误:在打开一个Optional值时出乎意料地发现了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
    }
}

我将视图控制器定义为自定义类。 enter image description here

我该怎么做才能正常工作? 请给我一个建议。

ADD)

ChatViewController由另一个类文件调用。

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

我相信@IBOutlet正确连接。

ChatViewController.swift

enter image description here

ChatViewController.storyboard

enter image description here

我知道调用此函数时tableView为nil。

enter image description here 实际上,我对故事板了解不多,因为我通常不会使用故事板。我更喜欢通过代码创建UI。但现在,我必须使用storyboard文件,因为我想使用聊天系统的示例项目。

ADD2)

我想我可能会误解故事板的设置。我不知道如何在故事板上使用导航控制器或入口点。

enter image description here

2 个答案:

答案 0 :(得分:1)

我解决了这个问题。

我修改了调用方法。

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

感谢您的善意建议。

答案 1 :(得分:0)

您应该在viewLoad()函数中添加委托和数据源。

 override func viewDidLoad() {
    super.viewDidLoad()

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

如果此后仍然存在错误,请检查"奥特莱斯"在你的故事板中。