0

I have a UITextField which calls a PopOver inside a function called popover() on Editing Did Begin.

Inside of the popover the user selects an item from a tableview, and presses ok.

On the OK press self.dismissViewControllerAnimated(true, completion: nil) is executed.

The problem is once the popover is dismissed the textfield isn't losing focus, it remains selected.

I am using resignFirstResponder at the beginning of the function to dismiss the keyboard.

I've tried using resignFirstResponder at the end of the function and I've tried having another element becomeFirstResponder but none of it is causing the textfield to lose focus.

How do I drop focus from the text field, and is there a way to detect the loss of focus (or closing of the popover) in order to automatically execute update()

My Code follows:

import UIKit

class ViewController: UIViewController, UIPopoverControllerDelegate, UIPopoverPresentationControllerDelegate {


//declare variables
var popOverLocation: CGRect!

// Set outlets for all buttons
@IBAction func update(sender: AnyObject) {
   texttest.text = newValue
}

@IBOutlet weak var texttest: UITextField!


@IBAction func testtexttouch(sender: AnyObject) {
    texttest.resignFirstResponder()
    popOverLocation = texttest.frame
    popOver()
    texttest.text = newValue
    texttest.resignFirstResponder()
}

func update() {
    texttest.text = newValue

}

//function for creating the popover
func popOver() {

    //Read the height, and XY coordinates of the active button
    var h = popOverLocation.height
    var x = popOverLocation.origin.x
    var y = popOverLocation.origin.y + h

    //create the popover
    var popoverContent = self.storyboard!.instantiateViewControllerWithIdentifier("Popover") as UIViewController
    var nav = UINavigationController(rootViewController: popoverContent)
    nav.modalPresentationStyle = UIModalPresentationStyle.Popover
    var popover = nav.popoverPresentationController! as UIPopoverPresentationController
    popover.sourceView = self.view
    popover.delegate = self

    //set the size of the popover
    popoverContent.preferredContentSize = CGSizeMake(200,300);

    //set the direction the popover arrow is allowed to point
    popover.permittedArrowDirections = UIPopoverArrowDirection.Up

    //using the buttons coordinates set the location of the popover
    popover.sourceRect = CGRectMake(x,y,0,0)
    self.presentViewController(nav, animated: true, completion: nil)
}


}
Kevin
  • 16,696
  • 7
  • 51
  • 68
BlueRad
  • 109
  • 2
  • 13

0 Answers0