iPhone开发【十六】实现点击一个UIImageView时打开键盘

转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/8230658  作者:张燕广

昨天发的一个帖子:http://bbs.csdn.net/topics/390295120?page=1#post-393027319

很多人不理解这样的需求,但是确实存在,具体就不细说了。

同样的需求,做Android客户端时在没有文本框时也可以通过inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);调出系统键盘。

但是,IOS中貌似没有这样的接口,所以可以采用“隐藏文本框”的方式,调出系统键盘,具体实现如下:

1、在ViewController.xib上放置一个ImageView和一个UITextField(代码中将其设置为隐藏),如下:

iPhone开发【十六】实现点击一个UIImageView时打开键盘_第1张图片

2、ViewController.h如下:

[cpp]  view plain copy
  1. <span style="font-size:18px;">//  
  2. //  ViewController.h  
  3. //  showKeyBoard  
  4. //  
  5. //  Created by Zhang Yanguang on 12-11-27.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface ViewController : UIViewController<UITextFieldDelegate>{  
  12.     BOOL hasOpenKeyBoard;//是否打开键盘  
  13. }  
  14.   
  15. @property(strong,nonatomic)IBOutlet UIImageView *imgView;  
  16. @property(strong,nonatomic)IBOutlet UITextField *textField;  
  17.   
  18. -(IBAction)showKeyBoard:(id)sender;  
  19. @end  
  20. </span>  

3、ViewController.m如下:

[cpp]  view plain copy
  1. <span style="font-size:18px;">//  
  2. //  ViewController.m  
  3. //  showKeyBoard  
  4. //  
  5. //  Created by Zhang Yanguang on 12-11-27.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. @interface ViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation ViewController  
  16. @synthesize imgView;  
  17. @synthesize textField;  
  18. - (void)viewDidLoad  
  19. {  
  20.     [super viewDidLoad];  
  21.     // Do any additional setup after loading the view, typically from a nib.  
  22.     //隐藏文本框  
  23.     textField.hidden = YES;  
  24.     //设置代理  
  25.     textField.delegate = self;  
  26.     hasOpenKeyBoard = false;  
  27. }  
  28.   
  29. - (void)viewDidUnload  
  30. {  
  31.     [super viewDidUnload];  
  32.     // Release any retained subviews of the main view.  
  33. }  
  34.   
  35. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  36. {  
  37.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  38. }  
  39.   
  40. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{  
  41.     NSLog(@"touchesBegan...");  
  42.     //[imgView becomeFirstResponder];  
  43.     if(!hasOpenKeyBoard){  
  44.         [textField becomeFirstResponder];  
  45.     }else{  
  46.         [textField resignFirstResponder];  
  47.     }  
  48.     hasOpenKeyBoard=!hasOpenKeyBoard;  
  49. }  
  50.   
  51. #pragma mark UITextFieldDelegate Methods  
  52. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{  
  53.     NSLog(@"string=%@",string);  
  54.     return YES;  
  55. }  
  56.   
  57. @end  
  58. </span>  

方法shouldChangeCharactersInRange中可以监听到键盘输入。

4、效果如下:

iPhone开发【十六】实现点击一个UIImageView时打开键盘_第2张图片

你可能感兴趣的:(iPhone开发【十六】实现点击一个UIImageView时打开键盘)