Implementing Uitextfielddelegate with Swift

Implementing UITextFieldDelegate with Swift

Xcode 6 (Beta 1) is not currently supporting autocomplete for non-implemented protocol methods/properties (for Swift).

Your best bet is to <CMD> - click on the protocol that isn't yet fully implemented to see what you're missing.

How do I implement UITextFieldDelegate

Your ViewController should be an UITextFieldDelegate. You then set the current ViewController as the delegate of the textField, in this case in viewDidLoad().

Implement textFieldShouldReturn so the textField delegates the return task (textFieldShouldReturn) to the ViewController. In this method call resignFirstResponder, this way the keyboard will disappear. Example:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var someTextField: UITextField!

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

override func viewDidLoad() {
super.viewDidLoad()

someTextField.delegate = self

}
}

UITextFieldDelegate: How to select which methods gets called for each individual TextField

You can check if its the field you want to use i delegate function

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == Password {
return true
}
return false
}

func textFieldDidEndEditing(_ textField: UITextField) {
if textField == phoneNumber {
// do something
}
}

Swift reusable UITextFieldDelegate

If your want to reuse CustomTextFieldDelegate through out the project, you should use a Singleton instance;

textField.delegate = CustomTextFieldDelegate.sharedInstance

and the class changes

class CustomTextFieldDelegate : NSObject, UITextFieldDelegate {

static let sharedInstance:CustomTextFieldDelegate = CustomTextFieldDelegate();

func textFieldDidBeginEditing(textField: UITextField) {
NSLog("textFieldDidBeginEditing ...");
}
//... other methods
} //F.E.

Create UITextFieldDelegate to Limit Character Count in Swift

Use this UITextField delegate for Zipcode and Int Limit Character count

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

if string.rangeOfCharacter(from: NSCharacterSet.decimalDigits.inverted) == nil{
let len = (textField.text?.characters.count)! + string.characters.count
if len <= 5 {
return true
}
}
return false
}

UITextField delegate method for check the max character limit.

public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
var oldlength : Int = 0
if textField.text != nil
{
oldlength = (textField.text?.count)!
}

let replaceMentLength : Int = string.count
let rangeLength : Int = range.length

let newLength : Int = oldlength - rangeLength + replaceMentLength

return newLength <= charaterLimit || false
}

implementing UITextFieldDelegate

My code is here, I don't know where you clerical error, but you can check my code to find out:

import UIKit

class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()
// register a nib like below
let nib:UINib = UINib.init(nibName: "AnimatedFormFieldTableViewCell", bundle: nil)

self.tableView.register(nib, forCellReuseIdentifier: "AnimatedFormFieldTableViewCell")

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

return 88.0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "AnimatedFormFieldTableViewCell", for: indexPath as IndexPath) as! AnimatedFormFieldTableViewCell

cell.setLabelText("Enter title")

return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

tableView.deselectRow(at: indexPath, animated: true)
}

}

The result:

Sample Image

So, just check the steps, where you go wrong.

Implementing UITextFieldDelegate in a separate class

Note the declaration of delegate:

unowned(unsafe) var delegate: UITextFieldDelegate?

MyTextFieldDelegate() is created, assigned to delegate, and then deallocated when createUI() returns. It is deallocated by ARC because nothing owns it. The problem you are experiencing is exactly what unsafe is warning you about.

You need to create a strong reference to your MyTextFieldDelegate instance. You also need to guarantee that the delegate is not deallocated until after the text field is deallocated.

Note the difference between this behavior and weak. If the delegate were weak instead of unowned(unsafe), then it would become nil and never get called, instead of crashing when it's called.

Where to set UiTextField delegate method in custom UiView

Firstly take a look at life cycle of the view. Depending on this it is possible to highlight that method awakeFromNib is quite suitable because:

The nib-loading infrastructure sends an awakeFromNib message to each
object recreated from a nib archive, but only after all the objects in
the archive have been loaded and initialized. When an object receives
an awakeFromNib message, it is guaranteed to have all its outlet and
action connections already established.



Related Topics



Leave a reply



Submit