我在用作enum
UITextFieldDelegate
枚举:
enum Position {
//Enum for Position variable in MemeTextDelegate
case Top, Bottom
init () { //set outside of enum to .Bottom if needed
self = .Top
}
UITextFieldDelegate类:
class MemeTextDelegate: NSObject, UITextFieldDelegate {
var position = Position()
var userEnteredText = false //Default value
override init () {
switch (position) {
case (.Top):
textField.text = "TOP"
default:
textField.text = "BOTTOM"
}
}
//Methods removed for this example...
}
在ViewController
中,我设置了每个TextField的委托,因此它知道它是'Top'或'Bottom'
@IBOutlet weak var topTextField: UITextField!
@IBOutlet weak var bottomTextField: UITextField!
let topTextFieldDelegate = MemeTextDelegate()
let bottomTextFieldDelegate = MemeTextDelegate()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Set Delegates
self.topTextField.delegate = self.topTextFieldDelegate
self.bottomTextField.delegate = self.bottomTextFieldDelegate
//Set individual fields delegate properties for TOP and BOTTOM
self.topTextFieldDelegate.position = .Top
self.bottomTextFieldDelegate.position = .Bottom
}
textField.text
中MemeTextDelegate
的每一行都会收到一条编译错误:“使用未解析的标识符'textField'”,而在方法中使用textField
则完全没有问题。< / p>
我确信我遗漏了一些基本的东西,所以任何输入都有帮助。
答案 0 :(得分:1)
根据您提供的代码,MemeTextDelegate不拥有textField的实例。据推测,它只会来自委托方法。但是,你在init方法中指的是它。