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