用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)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