简体   繁体   中英

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];

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.

Implement the UITextFieldDelegate and the method for it is

-(void) textFieldDidBeginEditing:(UITextField *)textField

The above method is fired when you touch the UITextField. You may then position the UIPopoverController (I'm guessing that is what you're using to show the view in a popup) and as soon as you're done there pass the values back to the UITextField. Hence the popover's/viewcontroller presented's delegate should be your textfield object.

EDIT: After seeing the other answer below it struck me that I forgot to tell you how to stop the keyboard from showing. Just make this the first line in the method I've mentioned above:

    [textField resignFirstResponder];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM