ios - UIAlertController in tableViewCell - Swift -


i have button in each cell in tableviewcell. when button clicked, in cell coding page had action working already. decided let users confirm before action starts, added uialertcontroller inside action. alert controller works fine in uiviewcontroller, in uitableviewcell code below got error message: "does not have member named presentviewcontroller". how can make work? code here. thanks

@ibaction func followbtn_click(sender: anyobject) {            if title == "following" {              var refreshalert = uialertcontroller(title: "sure?", message: "do want unfollow person?", preferredstyle: uialertcontrollerstyle.alert)             refreshalert.addaction(uialertaction(title: "yes", style: .default, handler: { (action: uialertaction!) in              var query = pfquery(classname: "follow")              query.wherekey("user", equalto: pfuser.currentuser()!.username!)             query.wherekey("usertofollow", equalto: usernamelbl.text!)              var objects = query.findobjects()              object in objects! {                  object.delete()              }              }))              refreshalert.addaction(uialertaction(title: "no", style: .default, handler: { (action: uialertaction!) in                 println("cancel unfollow")             }))              self.presentviewcontroller(refreshalert, animated: true, completion: nil)           /*  self.window?.rootviewcontroller?.presentviewcontroller(refreshalert, animated: true, completion: nil)              when use above make run, code runs nothing happens , warning below in output area               2015-07-02 08:57:22.978 mylast[968:200872] warning: attempt present <uialertcontroller: 0x7f849c04ed20> on <uinavigationcontroller: 0x7f8499c69020> view not in window hierarchy!              */            }          else {              println(“rest work”)            /*   rest of code */          }   } 

make sure tableview controller in hierarchy of navigation controller or embed in uinavigationcontroller. because uialertview deprecated in swift , there uialertviewcontroller , whenever require or present alert must have controller hierarchy.

you can show actionsheet using same code little changes uialertcontrollerstyle action sheet.

one suggestions once try remove handler code nil

alert.addaction(uialertaction(title: "ok", style: uialertactionstyle.default, handler: nil)) 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -