Useful_Investigator
Useful_Investigator

Reputation: 948

UITextField hide cursor after return is pressed

I want to have an editable title for my Navigation Bar. I found this code

let navigationTitlelabel = UITextField(frame: CGRectMake(0, 0, 200, 21))
    navigationTitlelabel.center = CGPointMake(160, 284)
    navigationTitlelabel.textAlignment = NSTextAlignment.Center
    navigationTitlelabel.textColor  = UIColor.whiteColor()
    navigationTitlelabel.text = defaultListName
    self.navigationController!.navigationBar.topItem!.titleView = navigationTitlelabel
    navigationTitlelabel.delegate = self

I have it in viewWillAppear. This is so I can change the title of the view controller. Then I implement these for the delegate:

func textFieldShouldReturn(textField: UITextField) -> Bool {
    resignFirstResponder()
    return true

}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let moc = appDelegate.managedObjectContext

    if textField.text != ""{
        //save to Core Data
    return true
    }
    return false
}

However when I press return, the cursor still blinks. The data IS saved, and if I reload the VC the title is the new one, but I can't get out of the "editing the title mode" even if I tap another button.

How can I solve this issue?

I'm testing this on the simulator, Swift 2.2

Upvotes: 0

Views: 116

Answers (1)

user3608500
user3608500

Reputation: 835

Modify textFieldShouldReturn with below code

    textField.resignFirstResponder()

Upvotes: 1

Related Questions