开发者

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 showin开发者_如何学运维g 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];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜