2011-03-01 143 views
7

您好我发现了一些像这样的问题,但他们谈论textView,我有ViewController,与scrollView其中有6个textfield和一个textView我想要一个功能,使键盘消失在完成/返回按钮click.I实现功能resign第一响应者,它隐藏我的键盘,当我点击scrollView外,但这不完全是我想要的,因为我想让它消失在按钮点击太。如何隐藏键盘在UIViewController返回按钮click-> iphone

感谢所有帮助

回答

14

套装创建一个符合UITextFieldDelegate协议的类,并使您的文本字段的委托成为此类的一个实例。实施方法:

- (BOOL)textFieldShouldReturn:(UITextField *)textField 

如下:

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

嗨,我发现出来,所以用文本框的一点是在viewDidLoad中添加此行:

textFieldOne.returnKeyType = UIReturnKeyDone; 
    textFieldCislo.delegate = self; 
textFieldTwo.returnKeyType = UIReturnKeyDone; 
    textFieldCislo.delegate = self; 
... 

而这种实现方法:

-(BOOL)textFieldShouldReturn:(UITextField *)theTextField { 

    if (theTextField == textFieldOne) { 
     [textFieldOne resignFirstResponder]; 
    } 
... 
} 
+2

添加一个“返回YES; “在底部去除警告。还要在界面中添加“”以删除更多警告。 – Sunkas 2013-01-10 09:17:48

2

相当多的时间追捕这是令后感,这是我放在一起,它的工作就像一个魅力。

.H

// 
// ViewController.h 
// demoKeyboardScrolling 
// 
// Created by Chris Cantley on 11/14/13. 
// Copyright (c) 2013 Chris Cantley. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController <UITextFieldDelegate> 

// Connect your text field to this the below property. 
@property (weak, nonatomic) IBOutlet UITextField *theTextField; 

@end 

的.m

// 
// ViewController.m 
// demoKeyboardScrolling 
// 
// Created by Chris Cantley on 11/14/13. 
// Copyright (c) 2013 Chris Cantley. All rights reserved. 
// 

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 



- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // _theTextField is the name of the parameter designated in the .h file. 
    _theTextField.returnKeyType = UIReturnKeyDone; 
    [_theTextField setDelegate:self]; 

} 

// This part is more dynamic as it closes any text field when pressing return. 
// You might want to control every single text field separately but that isn't 
// what this code do. 
-(void)textFieldShouldReturn:(UITextField *)textField 
{ 
    [textField resignFirstResponder]; 
} 


@end 
2

U可以通过点击视图中的任何地方使用这种方法来隐藏键盘

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self.view endEditing:YES]; 
}