新建一个工程,实现登陆系统,即登陆页面、注册页面、找回密码页面之间的切换。
1、创建视图对象loginContainerView(登陆页面),registContainerView(注册页面),passwordContainerView(找回密码页面),将3个视图作为window的子视图。默认显示登陆页面。
2、点击登陆页面的登陆按钮,验证是否登陆成功。
3、点击登陆页面的找回密码按钮,切换显示找回密码页面。
4、点击登陆页面的注册按钮,切换显示注册页面。
5、点击找回密码页面的取消按钮,切换返回登陆页面。
6、点击注册页面的取消按钮,却换返回登陆页面。
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc {
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]autorelease];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//创建注册页面
[self creatRegisterView];
//创建找回密码页面
[self creatFindPassView];
//创建登陆页面
[self creatLoginView];
return YES;
}
- (void)creatLoginView {
//登陆页面
UIView *loginView = [[UIView alloc]initWithFrame:_window.frame];
loginView.tag = 1000;
loginView.backgroundColor = [UIColor whiteColor];
[self.window addSubview:loginView];
[loginView release];
//用户名label
UILabel *userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 100, 80, 30)];
userNameLabel.text = @"用户名";
userNameLabel.backgroundColor = [UIColor whiteColor];
[loginView addSubview:userNameLabel];
[userNameLabel release];
//密码label
UILabel *passLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 150, 80, 30)];
passLabel.text = @"密码";
passLabel.backgroundColor = [UIColor whiteColor];
[loginView addSubview:passLabel];
[passLabel release];
//用户名textField
UITextField *userNameTF = [[UITextField alloc]initWithFrame:CGRectMake(130, 100, 200, 30)];
userNameTF.tag = 1001;
userNameTF.placeholder = @"请输入用户名";
userNameTF.clearButtonMode = UITextFieldViewModeAlways;
userNameTF.borderStyle = UITextBorderStyleRoundedRect;
[loginView addSubview:userNameTF];
userNameTF.keyboardType = UIKeyboardTypeDefault;//键盘类型
userNameTF.returnKeyType = UIReturnKeyDone;
//设置代理
userNameTF.delegate = self;
[userNameTF release];
//密码textField
UITextField *passTF = [[UITextField alloc]initWithFrame:CGRectMake(130, 150, 200, 30)];
passTF.tag = 1002;
passTF.placeholder = @"请输入密码";
passTF.secureTextEntry = YES;
passTF.clearButtonMode = UITextFieldViewModeAlways;
passTF.borderStyle = UITextBorderStyleRoundedRect;
[loginView addSubview:passTF];
passTF.delegate = self;//代理
[passTF release];
//登陆button
UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
loginButton.frame = CGRectMake(40, 200, 80, 30);
loginButton.backgroundColor = [UIColor whiteColor];
loginButton.titleLabel.font = [UIFont systemFontOfSize:18];//字号
loginButton.backgroundColor = [UIColor whiteColor];
[loginButton setTitle:@"登陆" forState:UIControlStateNormal];
// [loginButton addTarget:self action:@selector(pushLoginView) forControlEvents:UIControlEventTouchUpInside];
[loginButton addTarget:self action:@selector(pushLogin) forControlEvents:UIControlEventTouchUpInside];
[loginView addSubview:loginButton];
//注册button
UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
registerButton.frame = CGRectMake(150, 200, 80, 30);
registerButton.titleLabel.font = [UIFont systemFontOfSize:18];
registerButton.backgroundColor = [UIColor whiteColor];
[registerButton setTitle:@"注册" forState:UIControlStateNormal];
[registerButton addTarget:self action:@selector(pushRegister) forControlEvents:UIControlEventTouchUpInside];
[loginView addSubview:registerButton];
//找回密码button
UIButton *findPassButton = [UIButton buttonWithType:UIButtonTypeSystem];
findPassButton.frame = CGRectMake(260, 200, 80, 30);
findPassButton.titleLabel.font = [UIFont systemFontOfSize:18];
findPassButton.backgroundColor = [UIColor whiteColor];
[findPassButton setTitle:@"找回密码" forState:UIControlStateNormal];
[findPassButton addTarget:self action:@selector(pushFindPass) forControlEvents:UIControlEventTouchUpInside];
[loginView addSubview:findPassButton];
}
- (void)pushLoginView {
//找到登陆界面
UIView *loginView = [_window viewWithTag:1000];
//登陆页面提前
[_window bringSubviewToFront:loginView];
}
- (void)pushRegister {
UIView *registerView = [_window viewWithTag:1003];
[_window bringSubviewToFront: registerView];
}
- (void)pushFindPass {
UIView *findPassView = [_window viewWithTag:1004];
[_window bringSubviewToFront:findPassView];
}
- (void)pushLogin {
//登陆界面
UIView *loginView = [_window viewWithTag:1000];
//登陆页面上用户名输入框
UITextField *userTextField = (UITextField *)[loginView viewWithTag:1001];
//登陆页面上的密码输入框
UITextField *passTextField = (UITextField *)[loginView viewWithTag:1002];
if (userTextField.text.length == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alertView show];
[alertView release];
}else if (passTextField.text.length == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"message:@"密码不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alertView show];
[alertView release];
}else if ([userTextField.text isEqualToString:@"lanou"] == 1 && [passTextField.text isEqualToString:@"12345"] == 1) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登陆成功" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
[alertView release];
}else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名或者密码错误,请核对后再试" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
[alertView release];
}
}
- (void)creatRegisterView {
//注册界面
UIView *registerView = [[UIView alloc]initWithFrame:self.window.frame];
registerView.tag = 1003;
registerView.backgroundColor = [UIColor whiteColor];
[self.window addSubview:registerView];
[registerView release];
//label的创建
NSArray *array = @[@"用户名",@"密码",@"确定密码",@"电话",@"邮箱",@"地址"];
for (NSInteger i = 0;i < 6;i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(40, 80+60*i, 90, 40)];
label.text = [NSString stringWithFormat:@"%@:",array[i]];
label.textAlignment = NSTextAlignmentRight;
label.font = [UIFont systemFontOfSize:20];
label.backgroundColor = [UIColor whiteColor];
[registerView addSubview:label];
[label release];
//textField的创建
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(160, 80 +60*i, 160, 40)];
textField.font = [UIFont systemFontOfSize:20];
textField.placeholder = [NSString stringWithFormat:@"请输入%@",array[i]];
if (i == 2) {
textField.placeholder = @"请再次输入密码";
}
textField.backgroundColor = [UIColor whiteColor];
textField.borderStyle = UITextBorderStyleRoundedRect;
[registerView addSubview:textField];
textField.delegate = self;//代理
[textField release];
}
//注册button
UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
loginButton.frame = CGRectMake(70, 500, 100, 30);
loginButton.backgroundColor = [UIColor whiteColor];
loginButton.titleLabel.font = [UIFont systemFontOfSize:18];
[loginButton setTitle:@"注册" forState:UIControlStateNormal];
[registerView addSubview:loginButton];
//取消button
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
cancelButton.frame = CGRectMake(200, 500, 100, 30);
cancelButton.backgroundColor = [UIColor whiteColor];
cancelButton.titleLabel.font = [UIFont systemFontOfSize:18];
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
[cancelButton addTarget:self action:@selector(pushLoginView) forControlEvents:UIControlEventTouchUpInside];//跳转页面
[registerView addSubview:cancelButton];
}
- (void)creatFindPassView {
//找回密码界面
UIView *findPassView = [[UIView alloc]initWithFrame:_window.frame];
findPassView.tag = 1004;
findPassView.backgroundColor = [UIColor whiteColor];
[_window addSubview:findPassView];
[findPassView release];
//邮箱textfield
UITextField *mailTextField = [[UITextField alloc]initWithFrame:CGRectMake(80, 80, 250, 40)];
mailTextField.backgroundColor = [UIColor whiteColor];
mailTextField.font = [UIFont systemFontOfSize:20];
mailTextField.placeholder = @"请输入电子邮箱";
mailTextField.borderStyle = UITextBorderStyleRoundedRect;
[findPassView addSubview:mailTextField];
mailTextField.delegate = self;//代理
[mailTextField release];
//返回Button
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
backButton.frame = CGRectMake(80, 150, 100, 40);
backButton.backgroundColor = [UIColor whiteColor];
backButton.titleLabel.font = [UIFont systemFontOfSize:18];
[backButton setTitle:@"找回" forState:UIControlStateNormal];
[findPassView addSubview:backButton];
//取消button
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
cancelButton.frame = CGRectMake(220, 150, 100, 40);
cancelButton.backgroundColor = [UIColor whiteColor];
cancelButton.titleLabel.font = [UIFont systemFontOfSize:18];
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
[cancelButton addTarget:self action:@selector(pushLoginView) forControlEvents:UIControlEventTouchUpInside];//跳转页面
[findPassView addSubview:cancelButton];
}
//收回键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
转载于:https://blog.51cto.com/fanyuecheng/1654154