2 love loveec love_loveec 于 2012.10.29 14:54 提问

不能按返回键隐藏键盘
ios

我的代码如下:

.h文件中:

    @interface TouchLabelViewController : UIViewController<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *aTextField;        
-(IBAction)hideKeyboard:(id)sender;

.m文件:

-(IBAction)hideKeyboard:(id)sender{
    [(UITextField*)sender resignFirstResponder];
}

后来我还尝试了

.h文件:

- (void)viewDidLoad
{
    [super viewDidLoad];
    aTextField.delegate = self;
}
@property (strong, nonatomic) IBOutlet UITextField *aTextField;
-(BOOL) textFieldShouldReturn:(UITextField *)textField;

.m文件

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

    [aTextField resignFirstResponder];
    return YES;
}

但是按返回键的时候,还是不能隐藏键盘啊

3个回答

lwq421336220
lwq421336220   2012.12.27 17:22
已采纳
-(IBAction)hideKeyboard:(id)sender{
    [self. aTextField resignFirstResponder];
    [self  aaaa];
}
-(void)aaaa{
   你要返回按钮做的事情;
}

或者是

-(IBAction)hideKeyboard:(id)sender{
    [self  aaaa];
    你返回按钮要做的事情;
}
-(void)aaaa{
   [self. aTextField resignFirstResponder];
}
g989_1314125
g989_1314125   2012.10.29 15:25

在.h类中设置:

你的
ViewController : UIViewController

然后在创建TextField的地方delegat:

 myTextField.delegate = self;

最后,

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

默认执行影响UIModalPresentationFormSheet的可见性,因此为了隐藏键盘要重写:

  - (BOOL)disablesAutomaticKeyboardDismissal 
 {
   return NO; 
 }
MrsSeven7
MrsSeven7   2012.10.30 10:21

使用UITextField中的Delegate:

-(IBAction)hideKeyboard:(id)sender
{
    [your_textfield resignFirstResponder]; 
}
Csdn user default icon
上传中...
上传图片
插入图片