我有我的ViewController類實現UITextFieldDelegate。我沒有自動完成諸如textFieldShouldBeginEditing之類的funcs。這是XCode 6中的一個錯誤嗎?這是我的班級實施。用Swift實現UITextFieldDelegate
class ViewController: UIViewController, UITextFieldDelegate
我有我的ViewController類實現UITextFieldDelegate。我沒有自動完成諸如textFieldShouldBeginEditing之類的funcs。這是XCode 6中的一個錯誤嗎?這是我的班級實施。用Swift實現UITextFieldDelegate
class ViewController: UIViewController, UITextFieldDelegate
Xcode 6(Beta 1)目前不支持未實現的協議方法/屬性(用於Swift)的自動完成。
最好的選擇是<CMD> - click
關於尚未完全實施的協議,以查看您錯過的內容。
我發現了一點變通方法。只需進入文件檢查器,並在編輯文件時將類型設置爲Objective-C。自動完成將呈現Swift選項。
只需在構建時將類型切換回Swift即可。
那一個不錯的技巧 –
class ViewController: UIViewController,UITextFieldDelegate //set delegate to class
@IBOutlet var txtValue: UITextField //create a textfile variable
override func viewDidLoad() {
super.viewDidLoad()
txtValue.delegate = self //set delegate to textfile
}
func textFieldDidBeginEditing(textField: UITextField!) { //delegate method
}
func textFieldShouldEndEditing(textField: UITextField!) -> Bool { //delegate method
return false
}
func textFieldShouldReturn(textField: UITextField!) -> Bool { //delegate method
textField.resignFirstResponder()
return true
}
注意:在Swift 1.2中它應該是:'(textField:UITextField) - > Bool'沒有'!' – Rob
在我的情況下,由我添加在迅速類實現的範圍之外的委託方法和限制被稱爲委託方法的錯誤。
多一點SWIFTY是...
@IBOutlet weak var nameTF: UITextField! { didSet { nameTF.delegate = self } }
我有一個分號錯誤添加到手勢說法,這是負責調用view.endEditing(真),這反過來調用委託方法,作爲textFieldShouldBeginEditing。有趣的swift不會顯示有時添加分號的任何編譯時或運行時錯誤,刪除分號後,所有內容都可以正常工作。
// MARK: - --->文本字段代表
func textFieldDidBeginEditing(textField: UITextField) {
print("TextField did begin editing method called")
}
func textFieldDidEndEditing(textField: UITextField) {
print("TextField did end editing method called\(textField.text)")
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
print("TextField should begin editing method called")
return true;
}
func textFieldShouldClear(textField: UITextField) -> Bool {
print("TextField should clear method called")
return true;
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
print("TextField should end editing method called")
return true;
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
print("While entering the characters this method gets called")
return true;
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
print("TextField should return method called")
textField.resignFirstResponder();
return true;
}
Swift 3.0.1
// UITextField Delegates
func textFieldDidBeginEditing(_ textField: UITextField) {
}
func textFieldDidEndEditing(_ textField: UITextField) {
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return true;
}
func textFieldShouldClear(_ textField: UITextField) -> Bool {
return true;
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return true;
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return true;
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder();
return true;
}
斯威夫特3
@IBOutlet weak var yourNameTextfield: UITextField! {
didSet {
yourNameTextfield.delegate = self
}
}
extension YourNameViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
}
func textFieldDidEndEditing(_ textField: UITextField) {
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return true
}
func textFieldShouldClear(_ textField: UITextField) -> Bool {
return true
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return true
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder();
return true
}
}
在使用雨燕3.1版UITextFields的網點,做標記的變化。
import UIKit
class LoginViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var txtUserID: UITextField!
@IBOutlet var txtPwd: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
txtUserID.delegate = self
txtPwd.delegate = self
}
// UITextField Delegates
func textFieldDidBeginEditing(_ textField: UITextField) {
print("TextField did begin editing method called")
}
func textFieldDidEndEditing(_ textField: UITextField) {
print("TextField did end editing method called\(textField.text!)")
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
print("TextField should begin editing method called")
return true;
}
func textFieldShouldClear(_ textField: UITextField) -> Bool {
print("TextField should clear method called")
return true;
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
print("TextField should end editing method called")
return true;
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
print("While entering the characters this method gets called")
return true;
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
print("TextField should return method called")
textField.resignFirstResponder();
return true;
}
}
我不認爲Xcode的6目前支持在迅速 – Jiaaro
未執行的委託方法是自動完成,自動完成是非常靠不住在Xcode 6 ...簡單地實現在類的委託方法 – Jack