ios - How can I display a popup message in Swift that disappears after 3 seconds or can be cancelled by user immediatelly? -


in swift app have uiviewcontroller single button.

this button invokes function calls popup disappears after 3 seconds. also, after time prints message console. code of function follows:

func showalertmsg(title: string, message: string){       let alertcontroller = uialertcontroller(title: title, message: message, preferredstyle: .alert)     self.presentviewcontroller(alertcontroller, animated: true, completion: nil)     let delay = 3.0 * double(nsec_per_sec)     let time = dispatch_time(dispatch_time_now, int64(delay))     dispatch_after(time, dispatch_get_main_queue(), {         alertcontroller.dismissviewcontrolleranimated(true, completion: nil)     print("popup disappeared")     })  } 

that works fine, wanted introduce improvement. wanted add there button cancel popup , avoid displaying message in console. there way of displaying such popup user? - there way of showing in popup message counter number of seconds running out shows how time left until popup disappears?

you can use nstimer decrement counter, update alert view , dismiss alert view when counter reaches 0. code adapted objective-c answer

class viewcontroller: uiviewcontroller {      var alertcontroller: uialertcontroller?     var alerttimer: nstimer?     var remainingtime = 0     var basemessage: string?      override func viewdidload() {         super.viewdidload()         // additional setup after loading view, typically nib.     }      override func viewdidappear(animated: bool) {         super.viewdidappear(animated)                  self.showalertmsg("test alert", message: "this disappear in ", time: 5)     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }      func showalertmsg(title: string, message: string, time: int) {          guard (self.alertcontroller == nil) else {             print("alert displayed")             return         }          self.basemessage = message         self.remainingtime = time          self.alertcontroller = uialertcontroller(title: title, message: self.alertmessage(), preferredstyle: .alert)          let cancelaction = uialertaction(title: "cancel", style: .cancel) { (action) in             print("alert cancelled")             self.alertcontroller=nil;             self.alerttimer?.invalidate()             self.alerttimer=nil         }          self.alertcontroller!.addaction(cancelaction)          self.alerttimer = nstimer.scheduledtimerwithtimeinterval(1.0, target: self, selector: #selector(viewcontroller.countdown), userinfo: nil, repeats: true)                 self.presentviewcontroller(self.alertcontroller!, animated: true, completion: nil)          }      func countdown() {          self.remainingtime -= 1         if (self.remainingtime < 0) {             self.alerttimer?.invalidate()             self.alerttimer = nil             self.alertcontroller!.dismissviewcontrolleranimated(true, completion: {                 self.alertcontroller = nil             })         } else {             self.alertcontroller!.message = self.alertmessage()         }      }      func alertmessage() -> string {         var message=""         if let basemessage=self.basemessage {             message=basemessage+" "         }         return(message+"\(self.remainingtime)")     }      } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -