【发布时间】:2011-06-23 21:40:01
【问题描述】:
你好 我的应用程序中有两个 UITextFields,当我触摸除 UITextFields 之外的任何地方时,我想关闭键盘 我该怎么做?
【问题讨论】:
标签: iphone keyboard uitextfield numeric-keypad
你好 我的应用程序中有两个 UITextFields,当我触摸除 UITextFields 之外的任何地方时,我想关闭键盘 我该怎么做?
【问题讨论】:
标签: iphone keyboard uitextfield numeric-keypad
在视图控制器中保留对 UITextField 的引用并调用 [yourTextField resignFirstResponder]
【讨论】:
您需要在后台创建一个自定义按钮,然后将其连接到在您的UITextField 上调用resignFirstResponder 的IBAction 方法
类似的东西
编辑:
因为您想以编程方式添加按钮而不是使用此代码
- (void)viewDidLoad
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
button.buttonType = UIButtonTypeCustom;
[button addTarget:self action:(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[button release];
然后在此之后添加您的控制器代码。
【讨论】:
创建一个不可见的按钮,将其放在UITextField 后面,并在点击按钮时将resignFirstResponder 消息发送到您的文本字段。
【讨论】:
使用委托,
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
你也可以在你的控制器中创建一个方法
-(IBAction)editingEnded:(id)sender{
[sender resignFirstResponder];
}
然后在 IB 的 Connection Inspector 中将 Event "Did End On Exit" 连接到它。
【讨论】:
为此使用 tochesBegin 方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[yourFirstTextField resignFirstResponder];
[yourSecondTextField resignFirstResponder];
}
您不需要任何其他东西。当您触摸视图中的任何位置时,两个 textField 都会辞职,无需知道哪个有焦点。当您触摸 textField 时,textField 会自行打开键盘。
【讨论】:
如果您不使用界面生成器,您可以手动连接事件,如下所述: How can I programmatically link an UIView or UIImageView with an event like "touch up inside"?
如果您使用的是界面生成器,则无需创建背景按钮。
你可以这样做:
【讨论】:
You need to define the action for your button click this way
[infoButton addTarget:self action:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
And implement the click method ,as for example i have provided the animation on button click method.
-(void)backButtonClicked
{
[UIView beginAnimations:@"animation" context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
[self.navigationController popViewControllerAnimated:YES];
[UIView commitAnimations];
}
Hope this will help you ,the action is without using IB.Its all done through coding
【讨论】:
Ishu 的回答很好。但我认为你也应该尝试一下:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
【讨论】:
使用这个。
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 180; distace of keyboard up.
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
【讨论】:
通常在这种情况下,您将有一个滚动视图来保存用户界面的其余部分。如果您将该滚动视图的委托分配给您的视图控制器,并说您实现了协议UIScrollViewDelegate,那么只需添加以下方法即可完成工作:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.view endEditing:YES];
}
【讨论】: