Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Swift 4:</h2> <hr> <p>Subclass <code>UITextField</code>:</p> <pre><code>// MyTextField.swift import UIKit protocol MyTextFieldDelegate { func textFieldDidDelete() } class MyTextField: UITextField { var myDelegate: MyTextFieldDelegate? override func deleteBackward() { super.deleteBackward() myDelegate?.textFieldDidDelete() } } </code></pre> <p>Implementation:</p> <pre><code>// ViewController.swift import UIKit class ViewController: UIViewController, MyTextFieldDelegate { override func viewDidLoad() { super.viewDidLoad() // initialize textField let input = MyTextField(frame: CGRect(x: 50, y: 50, width: 150, height: 40)) // set viewController as "myDelegate" input.myDelegate = self // add textField to view view.addSubview(input) // focus the text field input.becomeFirstResponder() } func textFieldDidDelete() { print("delete") } } </code></pre> <hr> <h2>Objective-C:</h2> <hr> <p>Subclass <code>UITextField</code>:</p> <pre><code>//Header //MyTextField.h //create delegate protocol @protocol MyTextFieldDelegate &lt;NSObject&gt; @optional - (void)textFieldDidDelete; @end @interface MyTextField : UITextField&lt;UIKeyInput&gt; //create "myDelegate" @property (nonatomic, assign) id&lt;MyTextFieldDelegate&gt; myDelegate; @end //Implementation #import "MyTextField.h" @implementation MyTextField - (void)deleteBackward { [super deleteBackward]; if ([_myDelegate respondsToSelector:@selector(textFieldDidDelete)]){ [_myDelegate textFieldDidDelete]; } } @end </code></pre> <p>Now simply add <strong>MyTextFieldDelegate</strong> to your <code>UIViewController</code> and set your <code>UITextFields</code> <strong>myDelegate</strong> to <code>self</code>:</p> <pre><code>//View Controller Header #import "MyTextField.h" //add "MyTextFieldDelegate" to you view controller @interface ViewController : UIViewController &lt;MyTextFieldDelegate&gt; @end //View Controller Implementation - (void)viewDidLoad { //initialize your text field MyTextField *input = [[MyTextField alloc] initWithFrame:CGRectMake(0, 0, 70, 30)]; //set your view controller as "myDelegate" input.myDelegate = self; //add your text field to the view [self.view addSubview:input]; } //MyTextField Delegate - (void)textFieldDidDelete { NSLog(@"delete"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload