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

首先需要建一个与屏幕等大的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];
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值