不能在Swift

时间:2015-05-08 02:23:55

标签: ios swift enums delegates textfield

我在用作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.textMemeTextDelegate的每一行都会收到一条编译错误:“使用未解析的标识符'textField'”,而在方法中使用textField则完全没有问题。< / p>

我确信我遗漏了一些基本的东西,所以任何输入都有帮助。

1 个答案:

答案 0 :(得分:1)

根据您提供的代码,MemeTextDelegate不拥有textField的实例。据推测,它只会来自委托方法。但是,你在init方法中指的是它。