Search code examples
iphoneuitextfieldsubviewaddsubview

show subview when UITextField touched


I'm pretty new to iPhone development, so please excuse my ignorance. I've googled this a bit and so far come up with nothing useful. Here is what I would like to do:

I would like a subview to popup(with the rest of the screen showing in the background) when a UITextField is touched. The popup is a calculator UIView that I created in the IB. It seems it is better to have a popup show than a customized keyboard, due to Apple's developer guidelines.

Here is my question. How do I capture the touch on the UITextField and how do I show the subview?

I have tried things like below to show the subview, with no luck:

    CustomCalculator *customCalc = [[CustomCalculator alloc] initWithNibName:@"CustomCalculator" bundle:nil];

UIViewController *calcController = [self.customCalc.view];

[self.view addSubview:calcController.view];

Solution

  • Use the delegate method:

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
       //Add your subview here 
    
       return NO; //this will stop the keyboard from poping up
      }
    

    This way when someone taps the textfield, your view will popup instead of the keyboard. Now once the user is interacting with your view, you will have to manipulate the string in the textfield.text property directly as a reaction to the User tapping buttons in your view.