@图示:
#pragma mark - @主绘制画面
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
#pragma mark 1.设置View的背景图片
UIColor *bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"002.jpg"]];
[self.view setBackgroundColor:bgColor];
#pragma mark 2.绘制账号
self.userLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 120, 50, 40)];
self.userLabel.text = @"账号";
[self.userLabel setFont:[UIFont boldSystemFontOfSize:20.0]];
[self.view addSubview:self.userLabel];
[self.userLabel release];
self.userText = [[UITextField alloc]initWithFrame:CGRectMake(self.userLabel.right , self.userLabel.top,self.userLabel.width+170 , self.userLabel.height)];
self.userText.borderStyle = UITextBorderStyleRoundedRect;
self.userText.placeholder = @"手机号/会员名/邮箱";
self.userText.clearButtonMode = UITextFieldViewModeAlways; // 使输入框后面有个小"x" 用来执行一次性删除
self.userText.keyboardType = UIKeyboardAppearanceDefault;
self.userText.keyboardAppearance = UIKeyboardAppearanceAlert;
self.userText.returnKeyType = UIReturnKeyJoin;
self.userText.delegate = self;
[self.view addSubview:self.userText];
[self.userText release];
#pragma mark 3.绘制密码
_pwdLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 170, 50, 40)];
_pwdLabel.text = @"密码";
[_pwdLabel setFont:[UIFont boldSystemFontOfSize:20.0]];
[self.view addSubview:_pwdLabel];
[_pwdLabel release];
_pwdText = [[UITextField alloc]initWithFrame:CGRectMake(_pwdLabel.right, _pwdLabel.top, _pwdLabel.width+170, _pwdLabel.height)];
_pwdText.borderStyle = UITextBorderStyleRoundedRect;
_pwdText.placeholder = @"password";
_pwdText.secureTextEntry = YES;
_pwdText.clearButtonMode = UITextFieldViewModeAlways;
_pwdText.keyboardType = UIKeyboardTypeEmailAddress;
_pwdText.keyboardAppearance = UIKeyboardAppearanceAlert;
_pwdText.returnKeyType = UIReturnKeyJoin;
_pwdText.delegate = self;
[self.view addSubview:_pwdText];
[_pwdText release];
#pragma mark 4.绘制是否记住密码控件
_isLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 210, 100, 40)];
_isLabel.text = @"记住密码";
[_isLabel setFont:[UIFont boldSystemFontOfSize:20.0]];
[self.view addSubview:_isLabel];
[_isLabel release];
_isISwitch = [[UISwitch alloc]initWithFrame:CGRectMake(230, 215, 40, _isLabel.height)];
[_isISwitch addTarget:self action:@selector(openOrClosed:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_isISwitch];
#pragma mark 5.绘制登录按钮
_login = [UIButton buttonWithType:UIButtonTypeSystem];
_login.frame = CGRectMake(40, 250, 240, 40);
[_login.titleLabel setFont:[UIFont boldSystemFontOfSize:30.0]]; // 设置字体大小
_login.tintColor = [UIColor blackColor];
[_login setBackgroundImage:[UIImage imageNamed:@"login1.png"] forState:UIControlStateNormal]; // 设置按钮的背景图片
[_login setTitle:@"登录" forState:UIControlStateNormal];
[_login addTarget:self action:@selector(loginView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_login];
}
#pragma mark - @响应是否记住密码开关
- (IBAction)openOrClosed:(id)sender{
UISwitch * switchButton = (UISwitch *)sender;
if ([switchButton isOn]) {
NSLog(@"YES");
} else {
NSLog(@"NO");
}
}
#pragma mark - @响应点击登录按钮
- (IBAction)loginView:(id)sender{
HMTMainViewController * mainVC = [[HMTMainViewController alloc]init];
UINavigationController * mainNC = [[UINavigationController alloc]initWithRootViewController:mainVC];
// 模态视图控制器呈现出来时候的视觉效果
mainVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
// 模态视图控制器呈现方式,默认全屏
mainVC.modalPresentationStyle = UIModalPresentationFullScreen;
NSString * userString = self.userText.text;
NSString * pwdString = self.pwdText.text;
if ([userString isEqualToString:@"23423423424"] && [pwdString isEqualToString:@"hE2342340"] ) {
[self presentViewController:mainNC animated:YES completion:^{
NSLog(@"登陆成功");
}];
} else {
NSLog(@"用户名或者密码错误");
}
[mainNC release];
[mainVC release];
#pragma mark 第3种方式收回键盘
// 当前视图结束编辑
[self.view endEditing:YES];
}
#pragma mark - @3种方法来收回弹出来的键盘
#pragma mark 第1种方式来收回键盘 -------> 通过UITextField本身的协议来实现
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
// 放弃第一响应者
[textField resignFirstResponder];
return YES;
}
#pragma mark 第2种方式来收回键盘 -------> 通过重写UIResponder的方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// selfself.view.subviews 返回的是加载在View视图上的所有视图的数组
[self.view.subviews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[UITextField class]]) {
[obj resignFirstResponder];
}
}];
}