好像没什麽前言可说XD
就是把logout功能做出来~~
如此一来我们就可以测试登入/注册功能是否能成功跳转到相对应的页面,同时也开始开发profile view controller的部分。
首先引入相关的libraries。
import UIKit
import FirebaseAuth
接着,因为我们希望profile view能显示一些设定、登出之类的功能,因此同样选用table view。
接着我们将这个sign out
元件放入table view中,并将指派对象指定为自己。
class ProfileViewController: UIViewController {
@IBOutlet var tableView: UITableView!
let data = ["Sign Out"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.delegate = self
tableView.dataSource = self
}
}
刻意练习,先前都提过UIViewController
没有实作指派的功能,因此我们将额外继承其他的class,并实作出以下3个必要的方法。
extension ProfileViewController: UITableViewDelegate, UITableViewDataSource {
//numberofrow
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
//cellforrow
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = data[indexPath.row]
cell.textLabel?.textAlignment = .center
cell.textLabel?.textColor = .red
return cell
}
接着第三个,是点选该行之後的行为。
我们预期使用者点选sign out
之後,firebase auth应该要能够收到已登出的讯息,一来使得该使用者无法在app内有行为、二来该使用者可以再次登入。
因此我们使用addAction
,并在handler加入方法,通知firebase auth进行登出。
最後将画面跳转离开。
//selectrow
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let actionSheet = UIAlertController(title: "Sure to sign out",
message: "",
preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Sign out",
style: .destructive,
handler: { [weak self] _ in
guard let strongSelf = self else{
return
}
do {
try FirebaseAuth.Auth.auth().signOut()
let vc = LoginViewController()
let nav = UINavigationController(rootViewController: vc)
nav.modalPresentationStyle = .fullScreen // if not-> pop up as a card
strongSelf.present(nav, animated: true)
}
catch {
print("Fail to log out")
}
}))
actionSheet.addAction(UIAlertAction(title: "Cancel",
style: .cancel,
handler: nil))
present(actionSheet, animated: true)
}
}
若上述内容有误或可以改进的部分,欢迎留言以及提出任何指教~
谢谢 (´・∀・`)
今天我们来看一下Native Camp用户设定,看一下有没有遗漏什麽栏位没有设置的, 这边可以看到可...
“Any fool can write code that a computer can unde...
Background Goland从1.11版本起就开始导入了GO Module功能,这样也不需要再...
Angular [目标] 进入系统>>登入>>首页 1. VSCode 撰写...
「食神归位!」,从天上传来一个声音,「你本来是掌管烧菜的神仙,因触犯天条,而被罚落凡间受三十六劫、七...