ios - Button action done when touched outside of keyboard - Swift Xcode -


i using following code enter value in uitextfield 'item', on clicking button addbutton value in textfield appended array , clear textfield. have added code hide keyboard when touched outside.

the problem is, after typing word in textfield, touching outside, action in button in done, before button pressed

@iboutlet weak var item: uitextfield!  @ibaction func addbutton(sender: anyobject) {      todolist.append(item.text!)     item.text = "" }  override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) {      self.view.endediting(true)  } 

i don't know problem, because new swift,

  1. first delete the

     @iboutlet weak var item: uitextfield!    &      @ibaction func addbutton(sender: anyobject) {    todolist.append(item.text!)   item.text = ""  } 

02.then go story board click view controller , click last button sign of arrow right. check outlets. delete item , add button on list.

  1. then again add text field , add button view controller. click text field + ctrl ad connect line view controllers code, same addbutton ,use touch inside not touching outside

  2. add these functions code , run project.

    override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { self.view.endediting(true) 

    }

    func textfieldshouldreturn(textfield: uitextfield) -> bool {  textfield.resignfirstresponder() return true 

    }

hope these steps you. if not leave comment


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -