i'm new ios programming , playing around tutorial found online. trying include activityviewindicator in sign in view. when "sign in" button tapped, activityviewindicator should show , show hidden when sign in invalid. problem should put self.signinviewindicator.stopanimating();
when sign in invalid? have enabled hides when stopped
option.
@ibaction func signinbuttontapped(sender: anyobject) { let userusername = userusernametextfield.text let userpassword = userpasswordtextfield.text if(userusername.isempty || userpassword.isempty) { return} signinviewindicator.startanimating() // send user data server side let myurl = nsurl(string: "http://192.168.168.135:8080/userlogin.php") let request = nsmutableurlrequest(url:myurl!) request.httpmethod = "post" let poststring = "username=\(userusername)&password=\(userpassword)" request.httpbody = poststring.datausingencoding(nsutf8stringencoding) let task = nsurlsession.sharedsession().datataskwithrequest(request) { data, response, error in if error != nil { println("error=\(error)") return } var err: nserror? var json = nsjsonserialization.jsonobjectwithdata(data, options: .mutablecontainers, error: &err) as? nsdictionary if let parsejson = json { var resultvalue = parsejson["status"] as? string println("result: \(resultvalue)") var isusersignedin:bool = false if(resultvalue=="success") { isusersignedin = true // login successful nsuserdefaults.standarduserdefaults().setbool(isusersignedin, forkey: "isuserloggedin") nsuserdefaults.standarduserdefaults().synchronize() self.dismissviewcontrolleranimated(true, completion: nil) } else { self.signinviewindicator.stopanimating() var messagetodisplay:string = parsejson["message"] as! string! if(!isusersignedin) { messagetodisplay = parsejson["message"] as! string! } dispatch_async(dispatch_get_main_queue(), { // display alert message confirmation var myalert = uialertcontroller(title: "alert", message: messagetodisplay, preferredstyle: uialertcontrollerstyle.alert) let okaction = uialertaction(title: "ok", style: uialertactionstyle.default, handler:nil) myalert.addaction(okaction); self.presentviewcontroller(myalert, animated: true, completion: nil) }) } } } task.resume() }
you got rigth, can never update ui thread other main thread, displaying alert in main thread had move message "self.signinviewindicator.stopanimating()
" inside dispatch block:
dispatch_async(dispatch_get_main_queue(), { // display alert message confirmation self.signinviewindicator.stopanimating() var myalert = uialertcontroller(title: "alert", message: messagetodisplay, preferredstyle: uialertcontrollerstyle.alert) let okaction = uialertaction(title: "ok", style: uialertactionstyle.default, handler:nil) myalert.addaction(okaction); self.presentviewcontroller(myalert, animated: true, completion: nil) })
Comments
Post a Comment