UIModalPresentationFormSheet模式下resignFirstResponder无法隐藏键盘

弹出UIModalPresentationFormSheet模式
Java代码   收藏代码
  1. - (IBAction)buttonPressed:(id)sender  
  2. {  
  3.     NSLog(@"Show feedback view now!");  
  4.     UIViewController *fbsheet = [[FeedbackSheet alloc] initWithNibName:@"FeedbackSheet" bundle:nil];  
  5.     fbsheet.modalPresentationStyle = UIModalPresentationFormSheet;  
  6.     [self presentModalViewController:fbsheet animated:YES];  
  7.       
  8.     [fbsheet release];  
  9. }  


效果如下:

UIModalPresentationFormSheet模式下resignFirstResponder无法隐藏键盘_第1张图片

但是当在UItextview软盘弹出之后,点击其他区域使用下面代码软盘无法隐藏
Java代码   收藏代码
  1. [feedbackContent resignFirstResponder];  


进查询资料之后发现重写disablesAutomaticKeyboardDismissal即可,该api在iOS (4.3 and later)
Java代码   收藏代码
  1. -(BOOL)disablesAutomaticKeyboardDismissal  
  2. {  
  3.     return NO;  
  4. }  


还有一种方式就是通过在键盘上添加toolbar,也是一种比较简易的操作
Java代码   收藏代码
  1.    - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0032050)];  
  6.     [topView setBarStyle:UIBarStyleBlack];  
  7.   
  8.     UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];  
  9.     UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle:@"收起键盘" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];  
  10.     [doneButton setWidth:80];  
  11.       
  12.     NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace,doneButton,nil];  
  13.     [doneButton release];  
  14.     [btnSpace release];  
  15.       
  16.     [topView setItems:buttonsArray];  
  17.     [feedbackContent setInputAccessoryView:topView];  
  18. }  
  19.   
  20. -(IBAction)dismissKeyBoard  
  21. {  
  22.     [feedbackContent resignFirstResponder];  
  23. }  


效果如下:

UIModalPresentationFormSheet模式下resignFirstResponder无法隐藏键盘_第2张图片


参考资料: http://stackoverflow.com/questions/3019709/modal-dialog-does-not-dismiss-keyboard

你可能感兴趣的:(java,ios,api)