【发布时间】:2017-03-05 09:26:39
【问题描述】:
上下文是,当用户点击登录按钮时,应用程序将使用 Web 服务检查凭据,如果用户被授权,则用户名和密码存储在 userDefaults 中(当用户再次打开应用程序时自动登录)
现在我在下一页使用注销按钮(用户在成功登录后进入的位置),这是我的注销按钮的代码,它不起作用。
据我说,点击注销时,它应该删除 userDefaults,然后返回登录页面。 目前我不关心外观,只是帮我写代码。
添加按钮的功能。
override func viewDidLoad()
{
super.viewDidLoad()
let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.trash, target: nil, action: #selector(self.logoutButtonTapped(_:)));
navigationItem.rightBarButtonItem = doneItem;
// Do any additional setup after loading the view.
}
注销按钮功能:
func logoutButtonTapped(_ sender: Any)
{
let defaults = UserDefaults.standard
defaults.removeObject(forKey: "userName")
defaults.removeObject(forKey: "userPassword")
let newViewObject = storyboard?.instantiateViewController(withIdentifier: "LoginPageViewController") as! LoginPageViewController //LoginPageViewController is my login view file, and identifier also has the same name.
navigationController?.pushViewController(newViewObject, animated: true)
}
【问题讨论】:
-
你能展示你的 stroyboard 场景吗
-
将
target设置为self
标签: ios swift uibutton segue viewcontroller