开发者

Hide the keyboard when Enter is pressed in a UITextField

开发者 https://www.devze.com 2023-01-22 15:53 出处:网络
开发者_开发知识库I am new to iPhone development. I have a UITextField, in that when I press enter I need to hide the keyboard.
开发者_开发知识库

I am new to iPhone development.

I have a UITextField, in that when I press enter I need to hide the keyboard.

Any way to accomplish this?


Implement the UITextFieldDelegate protocol's textFieldShouldReturn: method, and return YES based on the object that is passed in.

- (BOOL) textFieldShouldReturn:(UITextField *) textField {
   [textField resignFirstResponder];
   return (textField == someTextField);
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


  • (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; }


First if the textfield global object is myTextField then <br>

In .h file <br> 

@interface MyScreen : UIViewController <**UITextFieldDelegate**>
{
     UITextField *myTextField;
}
@property (nonatomic,retain) IBOutlet UITextField ***myTextField**;<br>

In .xib <br>

Attach myTextField to the control.<br>

In .m File <br>



- (void) viewDidLoad<br>
{

       [super viewDidLoad];
       myTextField.delegate = self;
}

If you want to return the keyboard on particular return button of keyboard press: <br>

#pragma mark  UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

If you want to return on your particular button <br>

- (IBAction) myButtonPressed:(id)sender
{
        [self.myTextField resignFirstResponder];
}


Apologise for my bad english.

Thanks & Regards,
Gautam Tiwari
Ios Application Developer
0

精彩评论

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