saman01
saman01

Reputation: 1004

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

Upvotes: 0

Views: 811

Answers (3)

David Dunham
David Dunham

Reputation: 8329

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

-textField:shouldChangeCharactersInRange:replacementString:

Try providing that one.

Upvotes: 2

Valeriy Van
Valeriy Van

Reputation: 1865

  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.

Upvotes: 0

Assad Ullah
Assad Ullah

Reputation: 785

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.

Upvotes: 0

Related Questions