2015-08-27 14 views

Odpowiedz

29

Wygląda na to, że opcja "Touch Up Inside" nie jest włączona dla UiTextField, ale działa funkcja "Touch Down".

więc rozwiązanie jest następujące:

myTextField.addTarget(self, action: #selector(myTargetFunction), for: UIControlEvents.touchDown) 

@objc func myTargetFunction(textField: UITextField) { 
    print("myTargetFunction") 
} 
1

Ustaw pełnomocnik UITextField do kontrolera widoku, wdrożenie metody delegata

-(void)textField:(UITextField*)textField didBeginEditing { 
    // User touched textField 
} 
8

oto Swfit:

i nie trzeba używać „touchUpInside” wystarczy użyć metody delegata podobnie jak :

Dodać Zobacz kontroler delegata:

class ViewController: UIViewController, UITextFieldDelegate{ 

func textFieldDidBeginEditing(textField: UITextField) -> Bool { 
    if textField == myTextField { 
     return true // myTextField was touched 
    } 
} 

oto inne metody Delegat:

protocol UITextFieldDelegate : NSObjectProtocol { 

    optional func textFieldShouldBeginEditing(textField: UITextField) -> Bool // return NO to disallow editing. 
    optional func textFieldDidBeginEditing(textField: UITextField) // became first responder 
    optional func textFieldShouldEndEditing(textField: UITextField) -> Bool // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end 
    optional func textFieldDidEndEditing(textField: UITextField) // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called 

    optional func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool // return NO to not change text 

    optional func textFieldShouldClear(textField: UITextField) -> Bool // called when clear button pressed. return NO to ignore (no notifications) 
    optional func textFieldShouldReturn(textField: UITextField) -> Bool // called when 'return' key pressed. return NO to ignore. 
} 

z szybkimi docs:

struct UIControlEvents : RawOptionSetType { 
    init(_ rawValue: UInt) 
    init(rawValue: UInt) 

    static var TouchDown: UIControlEvents { get } // on all touch downs 
    static var TouchDownRepeat: UIControlEvents { get } // on multiple touchdowns (tap count > 1) 
    static var TouchDragInside: UIControlEvents { get } 
    static var TouchDragOutside: UIControlEvents { get } 
    static var TouchDragEnter: UIControlEvents { get } 
    static var TouchDragExit: UIControlEvents { get } 
    static var TouchUpInside: UIControlEvents { get } 
    static var TouchUpOutside: UIControlEvents { get } 
    static var TouchCancel: UIControlEvents { get } 

    static var ValueChanged: UIControlEvents { get } // sliders, etc. 

    static var EditingDidBegin: UIControlEvents { get } // UITextField 
    static var EditingChanged: UIControlEvents { get } 
    static var EditingDidEnd: UIControlEvents { get } 
    static var EditingDidEndOnExit: UIControlEvents { get } // 'return key' ending editing 

    static var AllTouchEvents: UIControlEvents { get } // for touch events 
    static var AllEditingEvents: UIControlEvents { get } // for UITextField 
    static var ApplicationReserved: UIControlEvents { get } // range available for application use 
    static var SystemReserved: UIControlEvents { get } // range reserved for internal framework use 
    static var AllEvents: UIControlEvents { get } 
} 

UITextField nie reaguje na "touchUpInside" zobaczyć po prawej stronie, znajdziesz to dopuszczalne zdarzenia kontrolne

+0

Dzięki @Lacerax, ale chciałbym, aby wykryć następnie użytkownik kliknie pole tekstowe. Ja już wykrywania edycję z tej funkcji 'myTextField.addTarget (self, działania:„myTargetFunction”, forControlEvents: UIControlEvents.EditingChanged)', bez konieczności stosowania UITextFieldDelegate –

+0

więc oznaczać akcent, ale nie wchodzić w pole tekstowe, prawda? – Loxx

+0

tak, dotknij pola tekstowego –

3

Aby to trochę jaśniejsze te rzeczy muszą być na swoim miejscu. Użyłem tego, aby to zrobić, jeśli użytkownik wpisał coś w mojej aplikacji tekstowej pola własnego, usunięto cokolwiek z pola tekstowego debetu.

  1. UITextFieldDelegate musi być zadeklarowane w widoku kontrolera tj klasy SecondViewController:
  2. funkcji detektora func myDebitDetector func myCreditDetector muszą być w klasie ViewController.
  3. put debit.addTarget i credit.addtarget pojawi się widok wewnętrzny.

  4. @IBOutlet weak var debit: UITextField! i @IBOutlet weak var credit: UITextField! to pola tekstowe na scenorysie i połączone z obiektem viewController.

    klasa SecondViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {

    @IBOutlet weak var credit: UITextField! 
    
    @IBOutlet weak var debit: UITextField! 
    
        func myDebitDetector(textfield: UITextField){ 
         print("using debit") 
         credit.text = "" 
    
        } 
    
        func myCreditDetector(textfield: UITextField) { 
         print("using cedit") 
         debit.text = "" 
        } 
    
        override func viewWillAppear(animated: Bool) { 
    
    
    
         debit.addTarget(self, action: "myDebitDetector:", forControlEvents: UIControlEvents.TouchDown) 
         credit.addTarget(self, action: "myCreditDetector:", forControlEvents: UIControlEvents.TouchDown) 
        .... 
    
        } 
    } 
    
3

Swift 3.0 Wersja:

textFieldClientName.addTarget(self, action: Selector(("myTargetFunction:")), for: UIControlEvents.touchDown) 
+0

To nie działa ... Dostaję: Wartość typu "UITextView" nie ma członka "addTarget" – drbobdugan

+0

to nie działa koleś proszę o pomoc :) –

7

Aktualizacja dla Swift 3

Oto kod Swift 3:

myTextField.addTarget(self, action: #selector(myTargetFunction), for: .touchDown) 

to funkcja:

func myTargetFunction() { 
    print("It works!") 
} 
3

Swift 3.1:

1) Tworzenie rozpoznawania gestu

let textViewRecognizer = UITapGestureRecognizer() 

2) Dodać do obsługi rozpoznawania:

textViewRecognizer.addTarget(self, action: #selector(tappedTextView(_:))) 

3) Dodaj do rozpoznawania do widoku tekstowego:

textView.addGestureRecognizer(textViewRecognizer) 

4) Dodanie obsługi do swojej klasie:

func tappedTextView(_ sender: UITapGestureRecognizer) { 
     print("detected tap!") 
} 
5

I, o którym mowa w UIControlEvents documentation from Apple i wymyślił następujące:

Pierwszy dodać UITextFieldDelegate do klasy wtedy,

textBox.delegate = self 
textBox.addTarget(self, action: #selector(TextBoxOn(_:)),for: .editingDidBegin) 
textBox.addTarget(self, action: #selector(TextBoxOff(_:)),for: .editingDidEnd) 

z następujących funkcji:

func TextBoxOff(_ textField: UITextField) { 
    code 
     }     
} 

func TextBox(_ textField: UITextField) { 
    code 
     } 
} 
+0

Cała odpowiedź poza twoją odpowiedzią była dla mnie bezużyteczna! Tylko że zadziałało idealnie! Dzięki :) – Mansour

7

Swift 4:

class ViewController: UIViewController, UITextFieldDelegate { 
    func textFieldDidBeginEditing(_ textField: UITextField) { 
     if textField == myTextField { 
      print("You edit myTextField") 
     } 
    } 
} 

Ta funkcja pełnomocnika.