2011-02-09 40 views

回答

24

使用tochesBegin方法,一个有focus.and当你触摸文本框然后textField自己打开键盘。

-1

保持到的UITextField的引用您的视图控制器,并调用[yourTextField resignFirstResponder]

+0

什么呢你的意思参考,哪能代码,请解释一点 – 2011-02-09 04:40:12

+0

我的意思只是有一个IBOutlet到的UITextField,然后当有触摸,甚至其他任何地方,拨打[yourTextField resignFirstResponder] – 2011-02-09 21:31:42

1

您需要在后台创建自定义按钮,然后将其连接到IBAction方法调用在你的UITextField

resignFirstResponder

像这样 enter image description here

编辑:

你はNT以编程方式添加按钮比你可以使用此代码

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

,比在此之后添加控制器代码。

+0

我所有的编码是手动的,而不是基于接口的生成器,所以我怎么能叫一个简单的方法,你能告诉我吗? – 2011-02-09 05:00:51

+0

添加敲击手势代替 – 2016-03-18 21:36:56

1

创建一个不可见的按钮,将其放在UITextField的后面,并在轻按按钮时将resignFirstResponder消息发送到您的文本字段。

+0

,使透明按钮覆盖整个视图中,以使得其他任何触摸比在其顶部上的文本字段将激活按钮。 – hotpaw2 2011-02-09 04:45:06

1

使用委托,

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

你也可以在你的控制器

创建一个方法,然后在连接督察IB连接事件“真的结束在退出”来了。为了这个目的

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    [yourFirstTextField resignFirstResponder]; 
    [yourSecondTextField resignFirstResponder]; 
} 

你不需要else.When你随时随地触摸视图什么东西那么这两个文本框辞职没有必要知道

+0

我创造这样的方法,但现在如何调用它的任何地方点击,除了我的文本框时 - (无效){ba​​ckgroundTap \t [self.restaurant_name_textfield resignFirstResponder]; \t [self.amount_textfield resignFirstResponder]; } – 2011-02-09 04:46:04

0

如果你不使用界面生成器,你可以手动挂钩事件如下解释: How can I programmatically link an UIView or UIImageView with an event like "touch up inside"?

如果使用界面生成器,那么你就需要创建一个背景按钮。

你可以这样做:

  1. 选择你的观点(大概叫视图)
  2. 更改为第(i)在属性检查
  3. 更改类从UIView的到UIControl标签(这使你可以访问Touch Down事件)。
  4. 触摸下来事件到您的backgroundTap方法(Ctrl +拖动)。
  5. 不要忘了保存更改
0
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 
11

通过Ishu答案是一个很好的一个。但我认为你也应该尝试:

0

使用此。

- (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]; 
} 
1

通常在这种情况下,您将有一个滚动视图来保存用户界面的其余部分。如果您分配的滚动视图您的视图控制器的代表,并说你实现协议UIScrollViewDelegate,则只需添加下面的方法将做的工作:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 
{ 
    [self.view endEditing:YES]; 
} 
相关问题