#import "AppDelegate.h"
@interface AppDelegate ()<</span>UITextFieldDelegate>
@end
@implementation AppDelegate
- (void)dealloc{
self.window = nil;
[ super dealloc];
- (void)dealloc{
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary*)launchOptions {
//主视图
//textField上的数据
———————————————————————————————————————
——————————————————————————————————
}
=================================================
//点击空白回收键盘
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
}
//textField 点return回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField;{
[textField resignFirstResponder];
return YES;
}
}
例子:对上例题优化
建立LTView子类,继承自UIView
LTView.h
#import
@interface LTView : UIView
@property (nonatomic,retain)UILabel *label;
@property (nonatomic,retain)UITextField *field;
@property (nonatomic,retain)UILabel *label;
@property (nonatomic,retain)UITextField *field;
@end
LTView.m
@implementation LTView
//释放LT
- (void)dealloc{
self.label = nil;
self.field = nil;
[ super dealloc];
//释放LT
- (void)dealloc{
}
//重写父类的初始化方法
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
- (instancetype)initWithFrame:(CGRect)frame{
}
//封装成一个方法建议用_label,因为用seif.容易和后面的CGRectMake(self.)混淆
- (void)p_setup{
_label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width / 2,self.frame.size.height)];
// _label.backgroundColor = [UIColor blueColor];
[ self addSubview:_label];
[ _label release];
_field = [[UITextField alloc]initWithFrame:CGRectMake(self.frame.size.width / 2, 0,self.frame.size.width / 2, self.frame.size.height)];
// _field.backgroundColor = [UIColor cyanColor];
[ self addSubview:_field];
[ _field release];
//
//
}
UIView *contenView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
AppDelegate.m
记得引入子类头文件 #import "LTView.h"
//textField上的数据
// {(20,40 + 50*i),(280,30)}
//重写点击空白回收键盘
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// //获取父视图
UIView *contenView = [self.window viewWithTag:300];
for (int i = 0; i <</span> 5; i ++) {
//此时contentView 上的控件是我们自定义出来的LTView,所以根据Tag取出控件应该转换为LTView类型
LTView *view = (LTView *)[contenView viewWithTag:200 + i];
[view.field resignFirstResponder];
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
}
—————————————————————————————
//点击return收回键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField;{
[textField resignFirstResponder];
return YES;
}
欢迎学习本文档,未经博主允许,不得私自转载!