IOS键盘隐藏方法


在View的UITextField中经常需要输入完文字后隐藏软键盘,要实现着一点要让View的Controller实现UITextFieldDelegate代理,然后编写相应的代码。
方法一:点击视图空白处隐藏
#import
#import "SecondViewController.h"
@interface ViewController : UIViewController//需要同一协议才去执行协议

@property(retain,nonatomic)UITextField *filed1,*filed2;
@end


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
      [super viewDidLoad];
      //添加文本框
      self.fd1=[[UITextField alloc]initWithFrame:CGRectMake(80, 100, 150, 50)];
      self.fd1.borderStyle=UITextBorderStyleRounded Rect;
      self.fd1.textAlignment=UITextAlignmentLeft;//默认就是左
      [self.view addSubview:self.fd1];
      self.fd1.delegate=self;
     
      self.fd2=[[UITextField alloc]initWithFrame:CGRectMake(80, 180, 150, 50)];
      self.fd2.borderStyle=UITextBorderStyleRounded Rect;
      [self.view addSubview:self.fd2];
      self.fd2.delegate=self;
     
}

点击视图其他区域键盘隐藏
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.fd1 resignFirstResponder];
    [self.fd2 resignFirstResponder];
}

- (void)viewDidUnload
{
      [super viewDidUnload];
      // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterf aceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
      return (interfaceOrientation != UIInterfaceOrientationPo rtraitUpsideDown);
}

@end
方法二:敲回车隐藏
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
      [textField resignFirstResponder];
      return Yes;
}
把上面的程序代码红色部分,换成上面的代码就可以了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值