开发者

checks for which UITextField will be triggered (textFieldShouldBeginEditing)

开发者 https://www.devze.com 2023-02-17 05:23 出处:网络
I have been trying to implement the checks for which UITextField will be triggered. Here are the results of my findings for first two text fields only.

I have been trying to implement the checks for which UITextField will be triggered. Here are the results of my findings for first two text fields only. This gives me two errors that says "sender undeclared"... Where am I doing wrong? Thanks in advance properties and synthesize are OK! val is the tag value of the buttons for a caLculator.(such 0,1,2,3,4,5,6,7,8,9)

//.h file

IBOutlet UITextField *textFieldLoanAmountDisplay;
IBOutlet UITextField *textFieldInitDepositDisplay;


// .m file

const NSString *textField1Code= @"1";
const NSString *textField2Code= @"2";


-(BOOL)textField1ShouldBeginEditing:(UITextField *)textFieldLoanAmountDisplay {
    if (textFieldLoanAmountDisplay == textField1Code) 
    {
        UIButton *buttonPressed = (UIButton *)sender;
        int val = buttonPressed.tag;
        if ( [textFieldLoanAmountDisplay.text compare:@"0"] == 0 ) {
            textFieldLoanAmountDisplay.text = [NSString stringWithFormat:@"%d", val ];
        } else {
            textFieldLoanAmountDisplay.text = [NSString stringWithFormat:@"%@%d", textFieldLoanAmountDisplay.text, val ];
        }
    }
    return NO;    
}

-(BOOL)textField2ShouldBeginEditing:(UITextField *)textFieldInitDepositDisplay {
    if (textFieldInitDepositDisplay == textField2Code) 
    {
        UIButton *buttonPressed = (UIButton *)sender;
        int val = buttonPressed.tag;
        if ( [textFieldInitDepositDisplay.text compare:@"0"] == 0 ) {
            textFieldInitDepositDisplay.text开发者_StackOverflow = [NSString stringWithFormat:@"%d", val ];
        } else {
            textFieldInitDepositDisplay.text = [NSString stringWithFormat:@"%@%d", textFieldInitDepositDisplay.text, val ];
        }
    }
    return NO;
}


You haven't really explained what your issue is but just by looking at your code, you are using the incorrect delegate method names. You don't need a separate textFieldShouldBeginEditing: for each of your UITextField instances.

In your view controller class interface file, make sure you are declaring you conform to UITextFieldDelegate methods with:

@interface XXXXX : XXXXXX <UITextFieldDelegate>

Then in your implementation, use

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

And if you setup your textField instances with different tags as you seem to indicate above, simply use a switch statement to find out which textField is calling the method:

switch (textField.tag)
{
    case tagValue1:
    // Implement your logic here
    break;
    case tagValue2:
    // Implement your logic here
    break;
    ...
}


Conform to the UITextFieldDelegate protocol correctly. textFieldShouldBeginEditing will be called then by both UITextFields. The UITextfield depends on the delegate pattern so it expects a certain method to be implemented by its delegate and that method has to named correctly. What you're trying to use is the target action pattern which is used by UIButtons for example.

To find out which one has been called you can use the UITextfield parameter which is passed to the method. Don't forget to set the delegate.

0

精彩评论

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