#import "AppDelegate.h"
#import "LTView.h"
#define kLabel_x 40
#define kLabel_Width 100
#define kHeight 30
@interface AppDelegate ()
@end
@implementation AppDelegate
//UIActionSheet 從下部彈出來的警示框
//警示框 UIAlertView 中間彈出
//使用LTView來創建登錄界面
-(void)createLoginViewByLTView{
//首先創建用戶名和密碼輸入的控件
//得到整個屏幕的寬度
float screenWidth = CGRectGetWidth(self.window.frame);
//得到ltView的寬度 ltView整體居中
float ltVWidth = screenWidth-40*2;
//label顯示內容的數組
NSArray *labelTextArray = [NSArrayarrayWithObjects:@"用戶名",@"密碼",nil];
//輸入框佔位字符數組
NSArray *placeHolderArray = [NSArrayarrayWithObjects:@"請輸入用戶名",@"輸入密碼",nil];
for (int i =0 ; i<2; i++) {
LTView *ltView = [[LTViewalloc]initWithFrame:CGRectMake(40,100+i*(40+20), ltVWidth,40)];
ltView.leftLabel.text = labelTextArray[i];
ltView.rightTextField.placeholder = placeHolderArray[i];
[self.windowaddSubview:ltView];
}
}
//"註冊"按鈕的點擊方法
-(void)jumpToRegisterViewAction:(UIButton *)sender{
}
//"登錄"按鈕的點擊方法
-(void)loginAction:(UIButton *)sender{
//得到用戶名、密碼的textField
//需要得到這兩個輸入框的父視圖,父視圖通過tag值得到它們
UIView *loginView = [sender superview];
UITextField *userNameTextField = (UITextField *)[loginViewviewWithTag:1000];
UITextField *pwdTextField = (UITextField *)[loginViewviewWithTag:1001];
//判斷用戶名、密碼是否輸入
NSString *userNameStr = userNameTextField.text;
NSString *pwdStr = pwdTextField.text;
//如果用戶沒有輸入內容
if (userNameStr ==nil || [userNameStrisEqualToString:@"<null>"]||(userNameStr.length ==0)) {
NSLog(@"請輸入用戶名");
//警示框
UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"友情提示"message:@"用戶名不能爲空"delegate:nilcancelButtonTitle:@"確定"otherButtonTitles:nil,nil];
//顯示警示框
[alertView show];
}else if(pwdStr ==nil || [userNameStrisEqualToString:@"<null>"]||(pwdStr.length ==0)) {
NSLog(@"請輸入密碼");
//警示框
UIAlertView *alertView1 = [[UIAlertViewalloc]initWithTitle:@"密碼提示"message:@"密碼錯誤!"delegate:nilcancelButtonTitle:@"確定"otherButtonTitles:nil,nil];
[alertView1 show];
}else if ([userNameStrisEqualToString:@"zhanghan"] &&[pwdStrisEqualToString:@"123456"]){
NSLog(@"登錄成功");
}else{
NSLog(@"用戶名或者密碼錯誤");
}
}
//"找回密碼"按鈕的點擊方法
-(void)jumpToFindPwdViewAction:(UIButton *)sender{
}
//創建一個登錄界面
-(void)creatLogView{
//創建登錄界面的背景頁面
UIView *logView = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
//頁面頂部標題
UILabel *titleLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,20, CGRectGetWidth(logView.frame),40)];
//設置標題
titleLabel.text = @"登錄";
//設置文字居中
titleLabel.textAlignment =NSTextAlignmentCenter;
//給文字設置背景顏色
[titleLabel setBackgroundColor:[UIColorgrayColor]];
titleLabel.textColor = [UIColorredColor];
[logView addSubview: titleLabel];
//設置背景顏色
[logView setBackgroundColor:[UIColorwhiteColor]];
NSArray *labelTextArray = [NSArrayarrayWithObjects:@"用戶名",@"密碼",nil];
NSArray *textArray = [NSArrayarrayWithObjects:@"請輸入用戶名",@"請輸入密碼",nil];
//由於所有的控件的位置都是有規律的,所以我們纔可以通過for循環創建
for (int i =0; i<2; i++) {
//每循環一次,創建一行控件,一行控件包括 label和 textfiled
UILabel *myLabel =[[UILabelalloc]initWithFrame:CGRectMake(kLabel_x,200+i*50,kLabel_Width,kHeight)];
myLabel.text = labelTextArray[i];
UITextField *myTextField = [[UITextFieldalloc]initWithFrame:CGRectMake(kLabel_x+kLabel_Width+20,200+i*50,200, kHeight)];
myTextField.tag = 1000+i;
myTextField.placeholder = textArray[i];
//如果是密碼輸入框,文字樣式不能是明文輸入,需要設置爲密碼樣式
if (i == 1) {
myTextField.secureTextEntry = YES;
}
//將創建好的控件添加到loginView上面
[logView addSubview:myLabel];
[logView addSubview:myTextField];
}
//"註冊"按鈕
UIButton *registerBtn = [UIButtonbuttonWithType:UIButtonTypeSystem];
//設置標題
[registerBtn setTitle:@"註冊"forState:UIControlStateNormal];
//設置frame
[registerBtn setFrame:CGRectMake(40,300, 80, 40)];
//設置點擊事件
[registerBtn addTarget:selfaction:@selector(jumpToRegisterViewAction:)forControlEvents:UIControlEventTouchUpInside];
//添加註冊按鈕
[logView addSubview:registerBtn];
//"登錄"按鈕
UIButton *loginBtn = [UIButtonbuttonWithType:UIButtonTypeSystem];
//添加標題
[loginBtn setTitle:@"登錄"forState:UIControlStateNormal];
//設置frame
[loginBtn setFrame:CGRectMake(140,300, 80, 40)];
//添加點擊事件
[loginBtn addTarget:selfaction:@selector(loginAction:)forControlEvents:UIControlEventTouchUpInside];
//添加登錄按鈕
[logView addSubview:loginBtn];
//"找回密碼"按鈕
UIButton *findPWdBtn = [UIButtonbuttonWithType:UIButtonTypeSystem];
//添加標題
[findPWdBtn setTitle:@"找回密碼"forState:UIControlStateNormal];
//設置frame
findPWdBtn.frame = CGRectMake(240, 300, 80, 40);
//添加點擊事件
[findPWdBtn addTarget:selfaction:@selector(jumpToFindPwdViewAction:)forControlEvents:UIControlEventTouchUpInside];
//添加找回密碼按鈕
[logView addSubview:findPWdBtn];
//將logView添加到window上面
// [self.window addSubview:logView];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
//創建登錄界面(調用):
[selfcreatLogView];
[selfcreateLoginViewByLTView];
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
return YES;
}
#import <UIKit/UIKit.h>
@interface LTView : UIView
//因爲外部需要給label賦值,並且可以在外部得到textField中輸入的值,所以label和textField需要聲明在.h文件中,這樣,外部可以調用。
@property (nonatomic,strong)UILabel *leftLabel;//自定義視圖左側的label
@property (nonatomic,retain)UITextField *rightTextField;//自定義視圖右側的textField
@end
#import "LTView.h"
@implementation LTView
//因爲labrl和TextField要根據LTView的大小來佈局,所以我們需要拿到LTView的frame,所以需要重寫LTView的初始化方法。
-(instancetype)initWithFrame:(CGRect)frame{
self = [superinitWithFrame:frame];
if (self) {
//在初始化方法中,爲LTView添加label和 textField
//初始化label。並且添加到LTView。 label和 textField的寬度比例大約爲1:2
//得到整體寬度
float sumWidth = CGRectGetWidth(frame);
//得到label的寬度
float labelWidth = (sumWidth-20)/3;
_leftLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,0, labelWidth,CGRectGetHeight(frame))];
[self addSubview:_leftLabel];
//初始化textField並添加到LTView
_rightTextField =[[UITextFieldalloc]initWithFrame:CGRectMake(labelWidth +20, 0,labelWidth *2,CGRectGetHeight(frame))];
[selfaddSubview:_rightTextField];
}
return self;
}