简体   繁体   中英

Textfield event is never called in delegate

In my interface I have:

@interface MainViewController : UIViewController <SKProductsRequestDelegate, SKPaymentTransactionObserver, UITextFieldDelegate> {
    UITextField *Stock;
    // ....
}

This is the implementation I have:

- (BOOL)Stock:(UITextField *)textField shouldChangeTextInRange:(NSRange)range  replacementText:(NSString *)text
{
    if([[textField text] isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }    
    return YES;
} 

In the viewDidLoad I have Stock.delegate = self ;

I am expecting this method is called after any character is typed in the text field. But this routine is never called. What is the problem?

Thanks

If this is a UITextField, you've just implemented a random method. The actual delegate method is

-textField:shouldChangeCharactersInRange:replacementString:

Try providing that one.

Did you wire the TextField (Stock in your case) Delegate to FileOwner ?

If not then try this in viewDidLoad method of the Controller

Stock.delegate = self;

Or you can just wire it in IB.

  1. Declare MainViewController conforming UITextFieldDelegate :

    @interface MainViewController : UIViewController < UITextFieldDelegate >

  2. To be called this method of UITextFieldDelegate should be declared as:

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

  3. In code or in IB MainViewController should be set to be delegate .

Correct and it will be fired as supposed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM