开发者

Textfield event is never called in delegate

开发者 https://www.devze.com 2023-04-06 20:25 出处:网络
In my interface I have: @interface MainViewController : UIViewController <SKProductsRequestDelegate, SKPaymentTransactionObserver, UITextFieldDelegate> {

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];
  开发者_JAVA技巧      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.

0

精彩评论

暂无评论...
验证码 换一张
取 消