Form Sheet的ViewController无法自动隐藏键盘的解决方法

Form Sheet的ViewController无法自动隐藏键盘的解决方法

 

在实现登录,注册等iPad界面时,经常要用到FormSheet形式的ViewController。最简单的比如说登录界面,有用户名,密码,登录按钮组成,即两个UITextField,一个UIButton组成,希望输入用户名,按键盘的return键时,跳到输入密码,输入密码后按键盘的return键,键盘消失,进入登录处理。在非FormSheet形式的时候,界面处理跟预期一致。但当是FormSheet形式的时候,键盘隐藏不了。代码如下

 

1、第一个ViewController有一个按钮,弹出一个FormSheet风格的视图(部分代码)。

- (IBAction)buttonPressed:(id)sender {

   

    DemoViewController *demoViewController = [[DemoViewController alloc] initWithNibName: @"DemoViewController" bundle: nil];

    UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController: demoViewController] autorelease];

    nav.modalPresentationStyle = UIModalPresentationFormSheet;

    [demoViewController release];

   

    [self presentViewController: nav animated: YES completion: nil];   

  

}

 

2、弹出的视图代码(部分代码):

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do anyadditional setup after loading the view from its nib.

   

    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)] autorelease];

   

    _tfName .delegate = self;

    _tfPassword.delegate = self;

}

 

- (void)done{

   

    [self dismissViewControllerAnimated: YES completion: nil];

}

 

 

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

   

    if (textField == _tfName) {

        [_tfPassword becomeFirstResponder];

        return YES;

    }

   

    [textField resignFirstResponder];  

   

   

    return YES;

}

 

- (IBAction)buttonLoginPressed:(id)sender {

   

    [_tfName resignFirstResponder];

    [_tfPassword resignFirstResponder];

}

 

 

http://stackoverflow.com/questions/3372333/ipad-keyboard-will-not-dismiss-if-modal-view-controller-presentation-style-is-ui

上找到了解决方法:

 

Be careful ifyou are displaying the modal with a UINavigationController. You then haveto set the disablesAutomaticKeyboardDismissal on thenavigation controller and not on the view controller. You can easily do thiswith categories.

File:UINavigationController+KeyboardDismiss.h

#import <Foundation/Foundation.h>

 

@interface UINavigationController(KeyboardDismiss)

 

-(BOOL)disablesAutomaticKeyboardDismissal;

 

@end

File:UINavigationController+KeyboardDismiss.m

#import "UINavigationController+KeyboardDismiss.h"

 

@implementation UINavigationController(KeyboardDismiss)

 

-(BOOL)disablesAutomaticKeyboardDismissal

{

    returnNO;

}

 

@end

Do not forget to import the category in the file where you use theUINavigationController.

 

 

将上面的类别方法放在使用UINavigationController的地方就可以了。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值