2014-06-11 3 views
16

Ich habe meine ViewController-Klasse, die UITextFieldDelegate implementiert. Ich habe keine automatische Vervollständigung für die Funktionen wie textFieldShouldBeginEditing. Ist das ein Fehler in XCode 6? Hier ist meine Klassenimplementierung.Implementieren von UITextFieldDelegate mit Swift

class ViewController: UIViewController, UITextFieldDelegate 
+1

Ich glaube nicht, Xcode 6 zur Zeit der automatische Vervollständigung von nicht implementierten Delegatmethoden in schnellen – Jiaaro

+1

unterstützt wird Ja, Auto abgeschlossen ist sehr wackelig in Xcode 6 ... einfach die Delegatmethoden in Ihrer Klasse implementieren – Jack

Antwort

7

Xcode 6 (Beta 1) unterstützt derzeit nicht zur automatischen Vervollständigung für nicht-implementierten Methoden Protokoll/Eigenschaften (Swift).

Ihre beste Wette ist <CMD> - click auf dem Protokoll, das noch nicht vollständig implementiert ist, um zu sehen, was Sie vermissen.

2

Ich fand ein wenig Workaround. Gehe einfach zum Datei-Inspektor und setze den Typ auf Objective-C, während du die Datei bearbeitest. Die automatische Vervollständigung zeigt Ihnen Swift-Optionen.

Wechseln Sie einfach den Typ zurück zu Swift, wenn Sie bauen.

+0

eines das ist netter Trick –

29
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 
} 
+0

Hinweis: In Swift 1.2 sollte es sein: '(textField: UITextField) -> Bool' ohne das'! ' – Rob

0

In meinem Fall habe ich versehentlich die Delegate-Methoden außerhalb des Bereichs der Klassenimplementierung in swift hinzugefügt, und das beschränkt die Delegate-Methoden aufgerufen werden.

16

Ein bisschen mehr swifty ist ...

@IBOutlet weak var nameTF: UITextField! { didSet { nameTF.delegate = self } } 
0

Ich hatte ein Semikolon versehentlich auf die Geste Anweisung hinzufügen, die für den Aufruf view.endEditing verantwortlich war (true), die die Delegatmethoden abwechselnd nennt solche als textFieldShouldBeginEditing. Interessanter swift zeigt keine Kompilierzeit oder Laufzeitfehler für Semikolons, die manchmal hinzugefügt werden. Nach dem Entfernen des Semikolons funktioniert alles einwandfrei.

4

// MARK: - ---> Textfeld Delegierten

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; 
} 
14
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; 
    } 
2

Swift 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 
    } 
} 
6

Während Swift Version 3.1 mit den Auslässen der UITextFields verwenden, sollten Sie die Änderungen markieren.

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; 
    } 
} 
Verwandte Themen