首页 > 代码库 > 用IOS做一个界面切换的效果(登录界面和注册界面和找回密码界面的切换)(用封装好的lable和textf创建界面)
用IOS做一个界面切换的效果(登录界面和注册界面和找回密码界面的切换)(用封装好的lable和textf创建界面)
创建一个类封装uitextfield和UIlabel (源代码.m文件)
#import "TLView.h"
@interface TLView ()
{
UILabel *_desLabel; //左边的lable
UITextField *_textField;//右边的
}
@end
@implementation TLView
//改写父类的初始化方法,处理相同的性能
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
// Initialization code
CGFloat width = frame.size.width;
CGFloat height = frame.size.height;
//UIlabel
_desLabel = [[UILabelalloc] initWithFrame:CGRectMake(0,0, 0.3 * width, height)];
_desLabel.font = [UIFontsystemFontOfSize:18];
_desLabel.textAlignment =NSTextAlignmentRight;
// _desLabel.backgroundColor = [UIColor lightGrayColor];
[self addSubview:_desLabel];
[_desLabel release];
//UITextfield
_textField = [[UITextFieldalloc] initWithFrame:CGRectMake(0.4 * width,0 , 0.6 * width , height)];
_textField.borderStyle =UITextBorderStyleRoundedRect;
// _textField.backgroundColor = [UIColor lightGrayColor];
_textField.autocorrectionType =UITextAutocorrectionTypeNo;
[self addSubview:_textField];
[_textField release];
}
return self;
}
//改写初始化方法,处理不同的部分
- (id)initWithFrame:(CGRect)frame labelText:(NSString *)labelText placeholder:(NSString *)placeholder textFieldText:(NSString *)textFieldText{
self = [selfinitWithFrame:frame];
if (self) {
//initialization code here..
_desLabel.text = labelText; //
_textField.placeholder = placeholder;
_textField.text = textFieldText;
}
return self;
}
//填写各种方法,处理不同的部分
//1,是否采用安全模式
- (void)setSecureEntry:(BOOL)secureEntry{
_textField.secureTextEntry = secureEntry;
}
//2,设置键盘类型;
- (void)setKeyBoardType:(UIKeyboardType)keyBoardType{
_textField.keyboardType = keyBoardType;
}
//3,设置textField代理
- (void)setDelegate:(id<UITextFieldDelegate>)delegate{
_textField.delegate = delegate;
}
//4,获取输入框中的文字
- (NSString *)text{
return_textField.text;
}
@end
源代码(.m文件)
#import "ZKJAppDelegate.h"
#import "TLView.h"
#define main_tag 1000;
#define youxiang_tag 1002;
#define zhuce_tag 1001;
@interface ZKJAppDelegate ()
{
UIView *mainView;
UIView *_loginView;
UIView *_registerView;
UIView *_huntforView;
}
@end
@implementation ZKJAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
// Override point for customization after application launch.
mainView = [[UIViewalloc] initWithFrame:CGRectMake(0,0, 320,568)];
mainView.backgroundColor = [UIColorlightGrayColor];
/**
* ///////////////////////////
*/
//第三张视图 找回密码
_huntforView = [[UIViewalloc] initWithFrame:CGRectMake(0,0, 320,500)];
_huntforView.backgroundColor = [UIColorwhiteColor];
[mainView addSubview:_huntforView];
[_huntforView release];
UITextField *emailTextField = [[UITextFieldalloc] initWithFrame:CGRectMake(50,100, 200, 30)];
emailTextField.placeholder = @"邮箱";
emailTextField.borderStyle =UITextBorderStyleRoundedRect;
emailTextField.autocorrectionType =UITextAutocorrectionTypeNo;
emailTextField.delegate = self;
[_huntforView addSubview:emailTextField];
[emailTextField release];
[selfcreatButton2];
//第二张视图 注册视图
_registerView = [[UIViewalloc] initWithFrame:CGRectMake(0,0, 320,568)];
_registerView.backgroundColor = [UIColorwhiteColor];
[mainView addSubview:_registerView];
[_registerViewrelease];
NSArray *lableTexts1 =@[@"用户:",@"密码:",@"确认密码:",@"手机号:",@"邮箱:",@"住址:"];
NSArray *placeholders1 =@[@"请输入用户:",@"请输入密码:",@"请输入确认密码:",@"请输入手机号:",@"请输入邮箱:",@"请输入住址:"];
NSInteger y1 = 50;
for (int i =0 ; i <= 4; i++ ) {
TLView *ltView = [[TLViewalloc]initWithFrame:CGRectMake(30,50 * i + y1 , 260,30) labelText: lableTexts1[i]placeholder:placeholders1[i] textFieldText:nil];
[ltView setDelegate:self];
if (i == 1 || i ==2) {
//设置安全模式
[ltView setSecureEntry:YES];
}
if (i ==3) {
[ltView setKeyBoardType:UIKeyboardTypeNumberPad];
}
ltView.tag = 100 + i;
[_registerView addSubview: ltView];
}
[selfcreatButton1];
//第一张视图 登录界面
_loginView = [[UIViewalloc] initWithFrame:CGRectMake(0,0, 320,568)];
_loginView.backgroundColor = [UIColorlightGrayColor];
_loginView.tag =main_tag;
[mainView addSubview:_loginView];
[_loginView release];
NSArray *lableTexts =@[@"用户:",@"密码:",@"确认密码:",@"手机号:",@"邮箱:",@"住址:"];
NSArray *placeholders =@[@"请输入用户:",@"请输入密码:",@"请输入确认密码:",@"请输入手机号:",@"请输入邮箱:",@"请输入住址:"];
NSInteger y = 50;
for (int i =0 ; i <= 1; i++ ) {
TLView *ltView = [[TLViewalloc]initWithFrame:CGRectMake(30,50 * i + y , 260,30) labelText: lableTexts[i]placeholder:placeholders[i] textFieldText:nil];
[ltView setDelegate:self];
if (i == 1 || i ==2) {
//设置安全模式
[ltView setSecureEntry:YES];
}
if (i ==3) {
[ltView setKeyBoardType:UIKeyboardTypeNumberPad];
}
ltView.tag = 100 + i;
[_loginView addSubview: ltView];
}
[selfcreatButton];
/**
* ///////////////////////////
*/
[self.windowaddSubview:mainView];
[mainView release];
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
return YES;
}
/**
* /////////////////////////////////////////////
*/
- (void)creatButton{
UIButton *regisButton = [UIButtonbuttonWithType:UIButtonTypeSystem];
regisButton.frame = CGRectMake(50, 400, 220, 40);
[regisButton setTitle:@"注册"forState:UIControlStateNormal];
regisButton.backgroundColor = [UIColorlightGrayColor];
[regisButton addTarget:selfaction:@selector(registerClick)forControlEvents:UIControlEventTouchUpInside];
[_loginView addSubview: regisButton];
// [regisButton release];
UIButton *loginButton = [UIButtonbuttonWithType:UIButtonTypeSystem];
loginButton.frame = CGRectMake(50, 300, 220, 40);
[loginButton addTarget:selfaction:@selector(loginClick)forControlEvents:UIControlEventTouchUpInside];
[loginButton setTitle:@"登录"forState:UIControlStateNormal];
[loginButton setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
loginButton.backgroundColor = [UIColorredColor];
loginButton.layer.cornerRadius =5;
[_loginView addSubview:loginButton];
// [cancleButton release];
UIButton *huntButton = [UIButtonbuttonWithType:UIButtonTypeSystem];
huntButton.frame = CGRectMake(50, 200, 220, 40);
[huntButton addTarget:selfaction:@selector(huntForClick)forControlEvents:UIControlEventTouchUpInside];
[huntButton setTitle:@"找回密码" forState:UIControlStateNormal];
[huntButton setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
huntButton.backgroundColor = [UIColorlightGrayColor];
huntButton.layer.cornerRadius =5;
[_loginView addSubview:huntButton];
}
/**
* //////////////////////////////////////////////////////////
*
* @param application ;
*/
- (void)creatButton1{
UIButton *regisButton = [UIButtonbuttonWithType:UIButtonTypeSystem];
regisButton.frame = CGRectMake(50, 400, 220, 40);
[regisButton setTitle:@"注册"forState:UIControlStateNormal];
regisButton.backgroundColor = [UIColorlightGrayColor];
[regisButton addTarget:selfaction:@selector(registerClick1)forControlEvents:UIControlEventTouchUpInside];
[_registerView addSubview: regisButton];
// [regisButton release];
UIButton *cancleButton = [UIButtonbuttonWithType:UIButtonTypeSystem];
cancleButton.frame = CGRectMake(50, 300, 220, 40);
[cancleButton addTarget:selfaction:@selector(cancelClick1)forControlEvents:UIControlEventTouchUpInside];
[cancleButton setTitle:@"取消"forState:UIControlStateNormal];
[cancleButton setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
cancleButton.backgroundColor = [UIColorlightGrayColor];
cancleButton.layer.cornerRadius =5;
[_registerView addSubview:cancleButton];
// [cancleButton release];
}
/**
* ////////////////////////////////////////////////////////////////
*/
- (void)creatButton2{
UIButton *regisButton = [UIButtonbuttonWithType:UIButtonTypeSystem];
regisButton.frame = CGRectMake(50, 400, 220, 40);
[regisButton setTitle:@"找回"forState:UIControlStateNormal];
regisButton.backgroundColor = [UIColorlightGrayColor];
[regisButton addTarget:selfaction:@selector(zhaoHui2)forControlEvents:UIControlEventTouchUpInside];
[_huntforView addSubview: regisButton];
// [regisButton release];
UIButton *cancleButton = [UIButtonbuttonWithType:UIButtonTypeSystem];
cancleButton.frame = CGRectMake(50, 300, 220, 40);
[cancleButton addTarget:selfaction:@selector(quXiao2)forControlEvents:UIControlEventTouchUpInside];
[cancleButton setTitle:@"取消"forState:UIControlStateNormal];
[cancleButton setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
cancleButton.backgroundColor = [UIColorlightGrayColor];
cancleButton.layer.cornerRadius =5;
[_huntforView addSubview:cancleButton];
// [cancleButton release];
}
// //////////////////////////////// //00000000000
- (void)loginClick{ //登录
NSLog(@"正在登录...");
}
- (void)huntForClick{ //找回密码
NSLog(@"找回密码");
[mainViewexchangeSubviewAtIndex:2withSubviewAtIndex:0];
}
- (void)registerClick{ //注册
[mainViewexchangeSubviewAtIndex:2withSubviewAtIndex:1];
NSLog(@"注册");
// [mainView exchangeSubviewAtIndex:2 withSubviewAtIndex:1];
}
// ////////////////////////////// 1111111111
- (void)registerClick1{ // 注册
}
- (void)cancelClick1{ //取消
[mainViewexchangeSubviewAtIndex:2withSubviewAtIndex:1];
}
// ////////////////////// 22222222222
- (void)zhaoHui2{ //找回
}
- (void)quXiao2{ //取消
[mainViewexchangeSubviewAtIndex:0withSubviewAtIndex:2];
}
- (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:.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)dealloc{
NSLog(@"释放内存");
[self.windowrelease];
[super dealloc];
}
@end