我有一個函數通過檢查返回的bool來確定是否應該提交表單。是否可以在swift中的函數中使用UITextFieldDelegate方法?
功能:
func signUpViewController(signUpController: PFSignUpViewController!, shouldBeginSignUp info: [NSObject : AnyObject]!) -> Bool {
var informationComplete = true
// loop through all of the submitted data
for (key, value) in info {
let fieldValue: AnyObject? = value
if (!fieldValue || fieldValue?.length == 0) { // check completion
informationComplete = false;
break;
}
}
// is this possible
textFieldDidEndEditing(textField: UITextField?) {
}
// Display an alert if a field wasn't completed
if (!informationComplete) {
var alert = UIAlertController(title: "Error", message: "Make sure you fill out all of the fields!", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
PFUser.logOut()
return informationComplete;
}
現在我檢查空字段,但需要檢查其他的東西,如在文本字段中的文本量,無論是電子郵件地址已被正確輸入。
我想用textFieldDidEndEditing已經離開後,檢查文本字段的內容。我將基本上使用if語句來執行檢查,然後顯示錯誤消息,但也會將informtionComplete設置爲false,以便不會提交任何錯誤。我可能也會禁用提交按鈕。
上述功能我會做這樣的事情的輸出端:
override func textFieldDidEndEditing(textField: UITextField!) {
if textField == self.signUpView.usernameField {
var length = countElements(self.signUpView.usernameField.text!)
if length < 2 {
var alert = UIAlertController(title: "Error", message: "Your company can only have a minimum of 2 characters!", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
self.signUpView.signUpButton.enabled = false
} else {
self.signUpView.signUpButton.enabled = true
}
}
if textField == self.signUpView.passwordField {
}
if textField == self.signUpView.emailField {
}
}
可以類似的功能內進行或者我需要設置實例瓦爾就像我會在Objective-C並從signUpViewController函數的外部設置informationComplete var?
爲什麼你需要實現內部的另一種方法文本框的委託方法? – Hokage 2014-09-03 12:48:37