iT邦幫忙

2021 iThome 鐵人賽

DAY 24
0
自我挑戰組

來寫看看app好了! Swift探索之旅系列 第 24

Day#24 尋找其他使用者(1)

前言

好的!開發日程進入尾聲,不過開發內容還沒有 (×ω×)
果然是前幾天太忙沒有很多進度的後果,哭哭

開啟新對話

ConversationViewController

現在已經可以有mock的對話內容了,接下來想要可以新增新的對話,因此我們在conversation主畫面加上一個右上角的按鈕,當按下按鈕時,可以進行尋找使用者的功能。

viewDidLoad

navigationItem.rightBarButtonItem = 
    UIBarButtonItem(barButtonSystemItem: .compose,
                    target: self,
                    action: #selector(didTapComposeButton))

然後,針對selector新增一個objc的方法。

@objc private func didTapComposeButton() {
    let vc = NewConversationViewController()
    let navVC = UINavigationController(rootViewController: vc)
    present(navVC, animated: true)
}

宣告一個新的view controller,在點選的時候,將新的controller顯示出來。

NewConversationViewController

好的~現在我們就來實作new conversation view controller的內容~~

首先引入相關函式庫

import UIKit
import JGProgressHUD
class NewConversationViewController: UIViewController {
    
    private let spinner = JGProgressHUD()

    private let searchBar: UISearchBar = {
        let searchBar = UISearchBar()
        searchBar.placeholder = "Search for users"
        return searchBar
    }()
    

在最上方加入搜尋bar,提示文字註明為搜尋使用者。

    private let tableView: UITableView = {
        let table = UITableView()
        table.isHidden = true
        table.register(UITableViewCell.self,
                       forCellReuseIdentifier: "cell")
        return table
    }()
    
    private let noResultLabel: UILabel = {
        let label = UILabel()
        label.isHidden = true
        label.text = "No result"
        label.textAlignment = .center
        label.textColor = .green
        label.font = .systemFont(ofSize: 21, weight: .medium)
        return label
    }()

當有資料時,就以table view的方式呈現,若沒有的話,則以ui label說明沒有相關的結果。

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        
        navigationController?.navigationBar.topItem?.titleView = searchBar
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Cancel",
                                                            style: .done,
                                                            target: self,
                                                            action: #selector(dismissSelf))
    }
    
    @objc private func dismissSelf() {
        dismiss(animated: true, completion: nil)
    }
}

最後我們給controller加一些設定、把search bar放入controller。
以及設定dismiss的功能。

結語

如此一來搜尋的UI就完成了! 明天有機會的話可以實作接DB的部分!
加油嗚嗚,行百里者半九十~

若上述內容有誤或可以改進的部分,歡迎留言以及提出任何指教~
謝謝 (´・∀・`)


上一篇
Day#23 github連結先來
下一篇
Day#25 尋找其他使用者(2) 資料結構
系列文
來寫看看app好了! Swift探索之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言