我正在创建一个仅使用正确日期的TextField。现在,当我在我的UITextField所在的同一个ViewController类中使用文本字段委托和操作方法时,它工作正常。但是,我必须使它像一个可重用的组件,可以通过拖放在任何项目中使用。我有两个文件 - MakeDate.swift和ViewController.swift
MakDate.swift -
import Foundation
import UIKit
class MakeDate: NSObject, UITextFieldDelegate {
var textField: UITextField!
var string: String!
var viewController: UIViewController!
let characterset = NSCharacterSet(charactersInString: "0123456789")
init?(textField: UITextField!) {
self.textField = textField
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
self.string = string
return true
}
func takeProperDateInput() {
textField.addTarget(textField, action: #selector(self.textChange(_:)), forControlEvents: .EditingChanged)
}
@IBAction func textChange(sender: UITextField) {
print("\(sender.text!)")
print("\(sender.text!) && \(self.string)")
if self.string == "" {
if sender.text?.characters.count == 2 {
let index = sender.text?.endIndex.advancedBy(-2)
sender.text = sender.text?.substringToIndex(index!)
}
else if sender.text?.characters.count == 5 {
let index = sender.text?.endIndex.advancedBy(-2)
sender.text = sender.text?.substringToIndex(index!)
}
}
if sender.text?.characters.count == 1 && self.string != "" {
if self.string.rangeOfCharacterFromSet(characterset.invertedSet) != nil {
let index = sender.text?.endIndex.advancedBy(-1)
sender.text = sender.text?.substringToIndex(index!)
}
else if Int(sender.text!) != 1 && Int(sender.text!) != 0 {
sender.text = "0" + sender.text! + "/"
}
}
else if sender.text?.characters.count == 2 && self.string != "" {
if Int(string) != 1 && Int(string) != 2 && Int(string) != 0{
let index = sender.text?.endIndex.advancedBy(-1)
sender.text = sender.text?.substringToIndex(index!)
}else {
sender.text = sender.text! + "/"
}
}
else if sender.text?.characters.count == 4 && self.string != "" {
if self.string.rangeOfCharacterFromSet(characterset.invertedSet) != nil {
let index = sender.text?.endIndex.advancedBy(-1)
sender.text = sender.text?.substringToIndex(index!)
}
}
else if sender.text?.characters.count == 5 && self.string != "" {
if self.string == "/" {
var yearComponent = sender.text?.componentsSeparatedByString("/")
let index = sender.text?.endIndex.advancedBy(-2)
sender.text = sender.text?.substringToIndex(index!)
sender.text = sender.text! + "0" + yearComponent![1] + "/"
}else {
var yearComponent = sender.text?.componentsSeparatedByString("/")
if Int(yearComponent![1]) > 31 {
let index = sender.text?.endIndex.advancedBy(-1)
sender.text = sender.text?.substringToIndex(index!)
}else if Int(yearComponent![1]) > 0 && Int(yearComponent![1]) < 32 {
sender.text = sender.text! + "/"
}else {
let index = sender.text?.endIndex.advancedBy(-1)
sender.text = sender.text?.substringToIndex(index!)
}
}
}
else if sender.text?.characters.count == 10 && self.string != "" {
let index = sender.text?.endIndex.advancedBy(-4)
if sender.text?.substringFromIndex(index!).rangeOfCharacterFromSet(characterset.invertedSet) != nil {
let index = sender.text?.endIndex.advancedBy(-4)
sender.text = sender.text?.substringToIndex(index!)
} else {
var yearComponent = sender.text?.componentsSeparatedByString("/")
if Int(yearComponent![2]) == 0 {
let index = sender.text?.endIndex.advancedBy(-4)
sender.text = sender.text?.substringToIndex(index!)
}
}
}
else if sender.text?.characters.count > 10 && self.string != "" {
let index = sender.text?.endIndex.advancedBy(-1)
sender.text = sender.text?.substringToIndex(index!)
}
}
}
和ViewController.swift -
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var dateInsert: UITextField!
var dateMaker: MakeDate!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
dateMaker = MakeDate(textField: self.dateInsert)
self.dateInsert.delegate = self.dateMaker
self.dateMaker.takeProperDateInput()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我得到的错误是unrecognized selector sent to instance
答案 0 :(得分:1)
使用默认属性创建一个textfield类,并在视图控制器中实现而不是nsobject,以将textfield作为参数发送:
class baseTextfield: UITextField, UITextFieldDelegate {
}
class ViewController: UIViewController{
@IBOutlet weak var dateInsert: UIBaseTextField!
}
属性属于文本字段,而不属于视图。