您的位置:首页 > 移动开发 > IOS开发

iOS新手入门之点击空白处回收键盘

2015-10-07 16:38 543 查看
首先需要建一个与屏幕等大的UIView, 然后把这个UIView添加到这个屏幕上, 把创建的UITextField作为UIView的子视图(添加到UIView上)

#import "AppDelegate.h"
// 定义宏 设置成与屏幕等宽 方便使用
#define kScreenWidth [UIScreen mainScreen].bounds.size.width

@interface AppDelegate ()

// 把view设置成属性, 便于下面方法的调用
@property (nonatomic, retain) UIView *view;

@end

@implementation AppDelegate
- (void)dealloc
{
[_window release];
[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.
self.window.backgroundColor = [UIColor whiteColor];
// 初始化上面设置成属性的view
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 设置view的背景颜色为白色
self.view.backgroundColor = [UIColor whiteColor];
// 把view添加到window上
[self.window addSubview:self.view];
// 这点先不释放view 在下面释放view
// 利用for循环 创建多个UITextField
for (int i = 0; i < 3; i++) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake((kScreenWidth - 200) / 2, 100 + i * 80, 200, 50)];
textField.backgroundColor = [UIColor greenColor];
textField.font = [UIFont systemFontOfSize:22];
// 把textField添加到设置成属性的那个view上
[self.view addSubview:textField];
[textField release];
}
// 释放view
[self.view release];
[self.window makeKeyAndVisible];
return YES;
}


点击空白键盘回收使用下面方法就行

非常简单的点击空白实现键盘回收的方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: