ios – 在swift中按下按钮后,在segue之前执行一些代码

ios – 在swift中按下按钮后,在segue之前执行一些代码,第1张

概述我有一个按钮进入另一个视图.我想在segue移动之前执行一些代码.我面临的问题是在代码有机会完成之前,segue会转到另一页.因此,在更改视图之前,不会更改“用户”默认值中的值. 我的问题是我如何才能运行代码并在完成后让segue开火? 这是我到目前为止: @IBAction func onLogoutClick(sender: AnyObject) { //clear all url c 我有一个按钮进入另一个视图.我想在segue移动之前执行一些代码.我面临的问题是在代码有机会完成之前,segue会转到另一页.因此,在更改视图之前,不会更改“用户”默认值中的值.
我的问题是我如何才能运行代码并在完成后让segue开火?
这是我到目前为止:

@IBAction func onlogoutClick(sender: AnyObject) {    //clear all url chache     NSURLCache.sharedURLCache().removeAllCachedResponses()    //null out everything for logout    email = ""    password = ""    self.logininformation.setobject(self.email,forKey: "email")    self.logininformation.setobject(self.password,forKey: "password")    self.logininformation.synchronize()    //self.vIEw = LoginVIEw}
解决方法 你有几个选择;

第一种是从IB中的按钮中删除动作,然后在UIVIEwController对象和下一个场景之间创建一个segue;

@IBAction func onlogoutClick(sender: AnyObject) {    //clear all url chache     NSURLCache.sharedURLCache().removeAllCachedResponses()    //null out everything for logout    email = ""    password = ""    self.logininformation.setobject(self.email,forKey: "password")    self.logininformation.synchronize()    self.performSegueWithIDentifIEr("logoutSegue",sender: self)}

或者你可以摆脱@IBAction方法并实现prepareForSegueWithIDentifIEr

overrIDe func prepareForSegue(segue: UIStoryboardSegue!,sender: AnyObject!) {   if segue.IDentifIEr == "logoutSegue"  {        //clear all url chache         NSURLCache.sharedURLCache().removeAllCachedResponses()        //null out everything for logout        email = ""        password = ""        self.logininformation.setobject(self.email,forKey: "email")        self.logininformation.setobject(self.password,forKey: "password")        self.logininformation.synchronize()   }}
总结

以上是内存溢出为你收集整理的ios – 在swift中按下按钮后,在segue之前执行一些代码全部内容,希望文章能够帮你解决ios – 在swift中按下按钮后,在segue之前执行一些代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1077764.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存