自定义控件,实现部分
1 - (id)initWithFrame:(CGRect)frame descriptionText:(NSArray *)inText/*需要输入两个字符串*/
2 {
3 self = [super init];//通过父类调用init初始化方法,产生一个对象,此处的self就是类的对象
4 //判断是否初始化成功,未初始化之前,self = nil
5 if (self)
6 {
7 UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width/3, frame.size.height)];
8 myLable.text = inText[0];
9 myLable.textColor = [UIColor blueColor];//字体颜色
10 [self addSubview:myLable];
11 UITextField *myText = [[UITextField alloc] initWithFrame:CGRectMake(frame.size.width/3, frame.origin.y, frame.size.width/3*2, frame.size.height)];
12 myText.placeholder = inText[1];
13 myText.borderStyle = UITextBorderStyleRoundedRect;//边框样式(圆角)
14 [self addSubview:myText];
15 }
16 return self;//self包括(super init、addSubview:myLablea、ddSubview:myText)
17 }
调用部分
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2 // Override point for customization after application launch.
3 //设置window(注意:该处必须设置)
4 self.window = [[UIWindow alloc] init];
5 self.window.frame = [[UIScreen mainScreen] bounds];//指定window大小跟屏幕大下一致
6 self.window.backgroundColor = [UIColor grayColor];//UIWindow中的方法,背景颜色
7
8 MyUIView *myUIViewInstance1 = [[MyUIView alloc] initWithFrame:CGRectMake(30, 100, 350, 30) descriptionText:@[@"用户名:",@"请输入手机号或邮箱"]];
9 MyUIView *myUIViewInstance2 = [[MyUIView alloc] initWithFrame:CGRectMake(30, 150, 350, 30) descriptionText:@[@"密码:",@"必须同时包含字符和数字"]];
10 [self.window addSubview:myUIViewInstance1];
11 [self.window addSubview:myUIViewInstance2];
12
13 [self.window makeKeyAndVisible];//显示(使其可见)
14 return YES;
15 }
基础部分
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2 // Override point for customization after application launch.
3 //程序入口
4 //设置window
5 self.window = [[UIWindow alloc] init];
6 // self.window.frame = CGRectMake(0, 0, 100, 100);//指定窗口位置和大小
7 self.window.frame = [[UIScreen mainScreen] bounds];//指定window大小跟屏幕大下一致
8 self.window.backgroundColor = [UIColor grayColor];//UIWindow中的方法,背景颜色
9 //设置view
10 //创建view1
11 UIView *view1 = [[UIView alloc] init];
12 // UIView *view1 = [[UIView alloc] initWithCoder:(NSCoder *)];
13 // UIView *view1 = [[UIView alloc] initWithFrame:(CGRect)];
14 view1.frame = CGRectMake(80, 80, 200, 200);//设置位置大小
15 view1.backgroundColor = [UIColor orangeColor];//背景颜色
16 //将view1加入window
17 // [self.window addSubview:view1];
18
19 NSLog(@"---%@",self.window.subviews);
20 //集合会自动对它的元素做retain操作
21 /*
22 hidden:隐藏
23 alpha:透明度
24 supwiew:父视图
25 tag:标记,便于索引(比如view的索引)
26 eg:
27 view1.tag = 10;
28 UIView *v = [self.window viewWithTag:10];//索引view1
29 */
30
31 //UIlable
32 UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(80, 20, 200, 30)];
33 myLable.backgroundColor = [UIColor whiteColor];
34 [self.window addSubview:myLable];
35
36 //UItext
37 UITextField *myText = [[UITextField alloc] initWithFrame:CGRectMake(120, 100, 190, 30)];
38 myText.borderStyle = UITextBorderStyleRoundedRect;//边框样式(圆角)
39 myText.placeholder = @"手机号/邮箱";//占位符
40 // [view1 addSubview:myText];
41 [self.window addSubview:myText];
42
43 //UIButton+点击事件
44 // UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(120, 150, 80, 30)];
45 UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];//标准系统按钮,圆角形
46 myButton.frame = CGRectMake(120, 150, 80, 30);
47 myButton.backgroundColor = [UIColor orangeColor];//橙黄色
48 [myButton setTitle:@"登陆" forState:UIControlStateNormal];//设置标题和状态(正常)
49 [myButton addTarget:self/*对象*/ action:@selector(prin/*处理程序*/) forControlEvents:UIControlEventTouchUpInside/*事件*/];//松手检测
50 [self.window addSubview:myButton];
51
52 //UIAlertView
53 // UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请妥善保管你的密码" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
54 // [myAlert show];
55
56
57 [self.window makeKeyAndVisible];//显示(使其可见)
58 return YES;
59 }
60
61 - (void)prin
62 {
63 // NSLog(@"登陆成功");
64 UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请妥善保管你的密码" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
65 [myAlert show];//显示
66 }