Skip to content

Instantly share code, notes, and snippets.

@mingsai
Created July 2, 2015 23:54
Show Gist options
  • Select an option

  • Save mingsai/7f79af4f5e7b34e86a91 to your computer and use it in GitHub Desktop.

Select an option

Save mingsai/7f79af4f5e7b34e86a91 to your computer and use it in GitHub Desktop.
A textfield delegate used to catch and throw messages related to textfield events
//
// MNGTextFieldHandler.swift
//
//
// Created by Tommie N. Carter, Jr., MBA on 6/18/15.
// Copyright © 2015 MING Technology. All rights reserved.
//
import UIKit
struct MNGTextFieldEvents {
static let NextButtonTappedForTextField = "MNGTextFieldHandler.NextButtonTappedForTextField"
}
class MNGTextFieldHandler: NSObject, UITextFieldDelegate {
var fields:[UITextField]? = []
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
return true
}
func textFieldDidBeginEditing(textField: UITextField) {
textField.backgroundColor = UIColor.yellowColor()
}
func textFieldDidEndEditing(textField: UITextField) {
textField.backgroundColor = UIColor.whiteColor()
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
return true
}
func textFieldShouldClear(textField: UITextField) -> Bool {
return false
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
return true
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
defaultCenter.postNotification(NSNotification(name: MNGTextFieldEvents.NextButtonTappedForTextField, object: textField))
return false
}
}
//private var kAssociationKeyNextField: UInt8 = 0
//
//extension UITextField {
// @IBOutlet var nextField: UITextField? {
// get {
// return objc_getAssociatedObject(self, &kAssociationKeyNextField) as? UITextField
// }
// set(newField) {
// objc_setAssociatedObject(self, &kAssociationKeyNextField, newField, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
// }
// }
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment