Chào mừng các bạn đến với series mẹo vặt trong swift cùng Tuan Nguyen kỳ tiếp theo


import UIKit
struct MenuItem {
let title : String
let gotoVCID: String
}
class SlideMenu: UIViewController {
@IBOutlet weak var slideTableView: UITableView!
var menuItem: [MenuItem] = [
MenuItem(title: "Screen 1", gotoVCID: "Screen1"),
MenuItem(title: "Screen 2", gotoVCID: "Screen2"),
MenuItem(title: "Screen 3", gotoVCID: "Screen3")
]
override func viewDidLoad() {
super.viewDidLoad()
self.slideTableView.estimatedRowHeight = 100
self.slideTableView.rowHeight = UITableViewAutomaticDimension
slideTableView.dataSource = self
slideTableView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension SlideMenu: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuItem.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "TableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
let tableviewcell = cell as! TableViewCell
tableviewcell.titleLBl.text = menuItem[indexPath.row].title
return cell
}
}
- Đầu tiên chúng ta tạo một mảng menuItem để hiển thị listScreen của chúng ta

Bước 2: Tạo thông báo và đưa mảng menuItem để MainScreen có thể push sang các màn hình khác
- Tiếp theo chúng ta viết hàm
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
sideMenuController?.toggle()
let gotoVCID = menuItem[indexPath.row].gotoVCID
NotificationCenter.default.post(name: NSNotification.Name("kuserdidselect"), object: gotoVCID)
}
- Ở đây hàm
sideMenuController?.toggle()
sẽ cho phép sideMenu đóng lại
- Tiếp theo chúng ta cần một phương pháp để tại MainScreen nhận biết thao tác tại sideMenu và thực hiện việc push đi. Ở đây chúng ta có thể dùng 2 cách: protocol hoặc dùng notificationcenter để thông báo. Ở đây mình sẽ dùng phương notfication để hướng dẫn các bạn.
- Tại mảng menuItem của mình mình có đặt biến gotoVCID đây là các indentifier của các screen khởi tạo. tại func tableview didselect các bạn có thể let một biến gotoVCID để lấy được các giá trị gotoVCID trong mảng để gởi theo
- Ý nghĩa của hàm
NotificationCenter.default.post(name: NSNotification.Name("kuserdidselect"), object: gotoVCID)
là khi dòng được chọn của tableview hàm này sẽ gởi một thông báo đi và đem theo biến gotoVCID.Bước 3: Nhận thông báo và thực hiện việc đẩy màn hình
- Qua fife qiản trị của màn hình chính( MainScreen )
import UIKit
class MainScreen: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(forName: NSNotification.Name("kuserdidselect"), object: nil, queue: nil) { notif in
let screen = notif.object! as! String
let displayVC = self.storyboard?.instantiateViewController(withIdentifier: screen )
self.navigationController?.pushViewController(displayVC!, animated: true)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
- Tại file quản trị màn hình chính chúng ta viết hàm lắng nghe để bắt sự kiện bên sideMenu sau đó push sang các mình khác dựa trên identifier lấy được dựa trên biến đi kèm với thông báo chúng ta đã gởi đi
- Như vậy chúng ta đã có thể tạo thành công 1 sideMenu có cách push tương tự như sideMenu của ứng dụng google Map