标签:
一: 效果图
效果描述:点击空白处快速回收键盘
二: 工程图
三:代码区
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (retain, nonatomic) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
self.window = nil;
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
RootViewController *rootView = [[RootViewController alloc] init];
UINavigationController *navigationVC = [[UINavigationController alloc]initWithRootViewController:rootView];
self.window.rootViewController = navigationVC;
[rootView release];
[navigationVC release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
@end
RootViewController.h
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
RootViewController.m
#import "RootViewController.h"
@interface RootViewController ()
@property (nonatomic , retain) UITextField *text1;
@property (nonatomic , retain) UITextField *text2;
@property (nonatomic , retain) UITextField *text3;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self showText];
}
#pragma mark - 创建控件
- (void)showText {
self.text1 = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 260, 40)];
_text1.borderStyle = 2;
self.text2 = [[UITextField alloc] initWithFrame:CGRectMake(30, 160, 260, 40)];
_text2.borderStyle = 2;
self.text3 = [[UITextField alloc] initWithFrame:CGRectMake(30, 220, 260, 40)];
_text3.borderStyle = 2;
[self.view addSubview:_text1];
[self.view addSubview:_text2];
[self.view addSubview:_text3];
[_text1 release];
[_text2 release];
[_text3 release];
}
#pragma mark - 回收键盘的核心代码
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.text1 resignFirstResponder];
[self.text2 resignFirstResponder];
[self.text3 resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
标签:
原文地址:http://www.cnblogs.com/li625317534/p/5051487.html