IOS--延迟加载

AppDelegate.m

 1 #import "AppDelegate.h"
 2 #import "TestController.h"
 3 
 4 @interface AppDelegate ()
 5 @property(nonatomic,strong)TestController *controller;
 6 @end
 7 
 8 @implementation AppDelegate
 9 
10 
11 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
12     // Override point for customization after application launch.
13     self.window = [[UIWindow alloc]init];
14 
15     self.controller = [[TestController alloc]init];
16 
17     self.window.rootViewController = self.controller;
18     
19     [self.window makeKeyAndVisible];
20     return YES;
21 }

 

 

TestView.h

1 #import <UIKit/UIKit.h>
2 
3 @interface TestView : UIView
4 @property(nonatomic,strong)UITextField *textField1;
5 @property(nonatomic,strong)UITextField *textField2;
6 @property(nonatomic,strong)UITextField *textField3;
7 @end

TestView.m

 1 #import "TestView.h"
 2 @interface TestView()
 3 
 4 @end
 5 @implementation TestView
 6 
 7 -(instancetype)initWithFrame:(CGRect)frame
 8 {
 9     self = [super initWithFrame:frame];
10     if (self) {
11         self.backgroundColor = [UIColor grayColor];
12     }
13     return self;
14 }
15 
16 /*
17  延迟加载
18  */
19 -(UITextField*)textField1
20 {
21     if (!_textField1) {
22         self.textField1 = [[UITextField alloc]init];
23         CGFloat x = self.bounds.size.width*0.5-75;
24         NSLog(@"%f",_textField1.bounds.size.width*0.5);
25         self.textField1.frame = CGRectMake(x, 20, 150, 30);
26         
27         self.textField1.borderStyle = UITextBorderStyleLine;
28         self.textField1.text = @"延迟加载";
29         _textField1.returnKeyType = UIReturnKeyNext;
30         [self addSubview:_textField1];
31         NSLog(@"dfddsfds");
32     }
33     return _textField1;
34 }
35 
36 /*
37  延迟加载
38  */
39 
40 -(UITextField*)textField2
41 {
42     if (!_textField2) {
43         self.textField2 = [[UITextField alloc]init];
44         CGFloat x = self.bounds.size.width*0.5-75;
45         NSLog(@"%f",_textField2.bounds.size.width*0.5);
46         self.textField2.frame = CGRectMake(x,self.textField1.frame.origin.y+self.textField1.frame.size.height+20,150, 30);
47         
48         self.textField2.borderStyle = UITextBorderStyleLine;
49         self.textField2.text = @"延迟加载!!!";
50         _textField2.returnKeyType = UIReturnKeyNext;
51         [self addSubview:_textField2];
52         NSLog(@"sdf");
53     }
54     return _textField2;
55 }
56 
57 /*
58  延迟加载
59  */
60 
61 -(UITextField*)textField3
62 {
63     if (!_textField3) {
64         self.textField3 = [[UITextField alloc]init];
65         CGFloat x = self.bounds.size.width*0.5-75;
66         NSLog(@"%f",_textField3.bounds.size.width*0.5);
67         self.textField3.frame = CGRectMake(x, self.textField2.frame.origin.y + self.textField2.frame.size.height+20, 150, 30);
68         
69         self.textField3.borderStyle = UITextBorderStyleLine;
70         self.textField3.text = @"延迟加载!!!!!!";
71         _textField3.returnKeyType = UIReturnKeyNext;
72         [self addSubview:_textField3];
73     }
74     return _textField3;
75 }
76 
77 @end

TestController.m

 1 #import "TestController.h"
 2 #import "TestView.h"
 3 
 4 @interface TestController()
 5 
 6 @property(nonatomic,strong)TestView *testView;
 7 @property(nonatomic,assign)NSUInteger *currentFieldIndx;
 8 @property(nonatomic,strong)NSArray *textFields;
 9 
10 @end
11 
12 @implementation TestController
13 
14 - (void)viewDidLoad
15 {
16     [super viewDidLoad];
17     self.view.backgroundColor = [UIColor brownColor];
18     CGFloat x = self.view.bounds.size.width*0.5 - self.view.bounds.size.width*0.9*0.5;
19     CGFloat y = self.view.bounds.size.height*0.5 - self.view.bounds.size.height*0.9*0.5
20     ;    self.testView = [[TestView alloc]initWithFrame:CGRectMake(x, y, self.view.bounds.size.width*0.9, self.view.bounds.size.height*0.9)];
21     [self.view addSubview:self.testView];
22     self.testView.textField1.delegate = self;
23     self.testView.textField2.delegate = self;
24     self.testView.textField3.delegate = self;
25 }
26 
27 -(void)textFieldDidBeginEditing:(UITextField *)textField
28 {
29     self.currentFieldIndx = [self.textFields indexOfObject:textField];
30 }
31 
32 -(BOOL)textFieldShouldReturn:(UITextField *)textField
33 {
34     if (_textFields.count <= ++_currentFieldIndx) {
35         _currentFieldIndx = 0;
36     }
37     UITextField *newField = [_textFields objectAtIndex:_currentFieldIndx];
38     if ([newField canBecomeFirstResponder]) {
39         [newField becomeFirstResponder];
40     }
41     return YES;
42     
43 }
44 
45 @end

转载于:https://www.cnblogs.com/yuge790615/p/5233689.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值