Search code examples
iostoolbarnumpad

Putting negative sign on iphone numberpad


I need to have a negative sign on my iphone numberpad. I figured out how to a button to a toolbar over the numberpad. Here's a code snippet:

UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
                       [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"pm.png"] style:UIBarButtonItemStylePlain target:self action:@selector(pm)],nil];

pm.png is a +/- graphic. Now, i have multiple UItextfields that could call this numberpad, say textfield1 textfield2 textfield3, etc.

How do I write the method pm such that it will add the negative sign to the right textfield. I'm missing something basic here. I want something like

-(void) pm {

}

but i don't know how to tell this method is the textfield that is currently being edited.

Thanks


Solution

  • You need to keep track of which text field is the first responder. One way to do this is to register for the UITextFieldTextDidBeginEditingNotification notification. This notification will include a reference to the text field that now has the focus. Keep track of this text field in an instance variable. Then your pm method can reference that instance variable.