Lenin S
Lenin S

Reputation: 5

Pass arguments in @selector method

I got error when passing arguments in @selector method.

This is my code:

-(void) accessoryView : (UITextField*) textField
{

    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    numberToolbar.barStyle = UIBarStyleBlackTranslucent;
    numberToolbar.items = @[[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad )],
                           [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                           [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad:textField:)]];


    [numberToolbar sizeToFit];
    textField.inputAccessoryView = numberToolbar;
}

-(void)doneWithNumberPad : (UITextField*) txt : (id) sender {

}

Upvotes: 0

Views: 276

Answers (2)

luckyShubhra
luckyShubhra

Reputation: 2751

You can take help of reference variable for your textfield. Declare a global property as textfield. Keep a refernce of currently active textfield and access it in every method as per your requirement.

UITextField *activeTextField;

// UITextField Delegates
-(void)textFieldDidBeginEditing:(UITextField *)textField {     
     activeTextField = textField;
}

You can use this activeTextField property in your class anywhere.

You need to define your class as delegate of UITextField.

self.yourTextField.delegate = self

Upvotes: 2

clemens
clemens

Reputation: 17722

First: Your selector is wrong. I should be action:@selector(doneWithNumberPad::) or renaming your second method to -(void)doneWithNumberPad : (UITextField*) txt textField: (id) sender.

BUT Second: You can't send custom, additional parameters with an action method. You should access the text field over a property. In your case you can store the current text field in a property, when the editing in the text fields begin. Your view controller should implement UITextFieldDelegate to receive this event.

Upvotes: 0

Related Questions