标签:
#import "ViewController.h"
@interface ViewController ()
//声明顺序:全局变量、属性、方法
- (void)initailizeUserInterface; //初始化用户界面
@end
- (void)initailizeUserInterface; //初始化用户界面
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initailizeUserInterface];
}
#pragma mark - init初始化
- (void)initailizeUserInterface {
//设置视图背景颜色
self.view.backgroundColor = [UIColor whiteColor];
//文本标签
UILabel *titLadbel = [[UILabel alloc]initWithFrame:CGRectMake(80, 80, 220, 30)];
titLadbel.backgroundColor = [UIColor clearColor];
//设置文本标签显示内容
titLadbel.text = @"QQ2015";
//设置文本对齐方式
titLadbel.textAlignment = NSTextAlignmentCenter;
//添加到主视图
[self.view addSubview:titLadbel];
//图片视图
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(140, 130, 100, 80)];
imageView.backgroundColor = [UIColor redColor];
//添加图片
imageView.image = [UIImage imageNamed:@"image.jpg"];
//设置圆角
imageView.layer.cornerRadius = 40;
//设置属性 超出视图部分不显示
imageView.clipsToBounds = YES;
[self.view addSubview:imageView];
//文本输入框
UITextField *text1 = [[UITextField alloc]initWithFrame:CGRectMake(80, 220, 220, 30)];
//设置边框样式
text1.borderStyle = UITextBorderStyleLine;
UITextField *text2 = [[UITextField alloc]initWithFrame:CGRectMake(80, 270, 220, 30)];
text2.borderStyle = UITextBorderStyleLine;
//清除模式
text1.clearButtonMode = UITextFieldViewModeWhileEditing;
text2.clearButtonMode = UITextFieldViewModeWhileEditing;
//占位符提示
text1.placeholder = @"请输入账号";
text2.placeholder = @"请输入密码";
//设置tag值 不能小于10
text1.tag = 100;
text2.tag = 101;
[self.view addSubview:text1];
[self.view addSubview:text2];
//按钮设置
UIButton *login = [UIButton buttonWithType:UIButtonTypeSystem];
//视图的位置和大小
login.frame = CGRectMake(160, 350, 60, 60);
login.backgroundColor = [UIColor brownColor];
//设置按钮标题以及类型
[login setTitle:@"登陆" forState:UIControlStateNormal];
//添加事件监听
// addTarget触发按钮对象 action触发方法 forControlEvents 触发按钮行为
[login addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:login];
UIView *testView = [[UIView alloc] init];
testView.bounds = CGRectMake(0, 0, CGRectGetWidth(text1.bounds), CGRectGetHeight(text1.bounds));
testView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMaxY(login.frame) + 30);
testView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:testView];
}
#pragma mark - responds event 触发事件
//处理按钮事件行为
- (void)respondsToButton : (UIButton *)sender {
NSLog(@"%@", NSStringFromSelector(_cmd));
//通过tag获取文本输入框
//父视图管理子视图
UITextField *accountField = (UITextField *)[self.view viewWithTag:100];
UITextField *passwordField = (UITextField *)[self.view viewWithTag:101];
//获取文本输入的内容
NSString *accountStr = accountField.text;
NSString *passwordStr = passwordField.text;
if ([accountStr isEqualToString:@"admin"] && [passwordStr isEqualToString:@"password"]) {
NSLog(@"登陆成功");
self.view.backgroundColor = [UIColor redColor];
} else {
NSLog(@"登陆失败");
}
}
#pragma mark - responds touch 触摸
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//当用户触摸屏幕停止编辑
[self.view endEditing:YES];
}
标签:
原文地址:http://www.cnblogs.com/summerpopo/p/4626942.html