用IOS做一個界面切換的效果(登錄界面和註冊界面和找回密碼界面的切換)(用封裝好的lable和textf創建界面) – iPhone手機開發技術文章 iPhone軟體開發教學課程

創建一個類封裝uitextfield和UIlabel (源代碼.m文件)

#import “TLView.h”

@interface TLView ()

{

UILabel *_desLabel;
//左邊的lable

UITextField *_textField;//右邊的

}

@end

@implementation TLView

//改寫父類的初始化方法,處理相同的性能

– (id)initWithFrame:(CGRect)frame

{

self = [super
initWithFrame:frame];

if (self) {

// Initialization code

CGFloat width = frame.size.width;

CGFloat height = frame.size.height;

//UIlabel

_desLabel = [[UILabel
alloc] initWithFrame:CGRectMake(0,
0, 0.3 * width, height)];

_desLabel.font = [UIFont
systemFontOfSize:18];

_desLabel.textAlignment =
NSTextAlignmentRight;

// _desLabel.backgroundColor = [UIColor lightGrayColor];

[self addSubview:_desLabel];

[_desLabel
release];

//UITextfield

_textField = [[UITextField
alloc] 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 = [self
initWithFrame: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 = [[UIWindow
alloc] initWithFrame:[[UIScreen
mainScreen] bounds]];

// Override point for customization after application launch.

mainView = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 320,
568)];

mainView.backgroundColor = [UIColor
lightGrayColor];

/**

* ///////////////////////////

*/

//第三張視圖 找回密碼

_huntforView = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 320,
500)];

_huntforView.backgroundColor = [UIColor
whiteColor];

[mainView
addSubview:_huntforView];

[_huntforView
release];

UITextField *emailTextField = [[UITextField
alloc] initWithFrame:CGRectMake(50,
100, 200,
30)];

emailTextField.placeholder =
@”郵箱”;

emailTextField.borderStyle =
UITextBorderStyleRoundedRect;

emailTextField.autocorrectionType =
UITextAutocorrectionTypeNo;

emailTextField.delegate =
self;

[_huntforView
addSubview:emailTextField];

[emailTextField release];

[self
creatButton2];

//第二張視圖 註冊視圖

_registerView = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 320,
568)];

_registerView.backgroundColor = [UIColor
whiteColor];

[mainView
addSubview:_registerView];

[_registerView
release];

NSArray *lableTexts1 =
@[@”用戶:”,@”密碼:”,@”確認密碼:”,@”手機號:”,@”郵箱:”,@”住址:”];

NSArray *placeholders1 =
@[@”請輸入用戶:”,@”請輸入密碼:”,@”請輸入確認密碼:”,@”請輸入手機號:”,@”請輸入郵箱:”,@”請輸入住址:”];

NSInteger y1 =
50;

for (int i =
0 ; i <= 4; i++ ) {

TLView *ltView = [[TLView
alloc]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];

}

[self
creatButton1];

//第一張視圖 登錄界面

_loginView = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 320,
568)];

_loginView.backgroundColor = [UIColor
lightGrayColor];

_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 = [[TLView
alloc]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];

}

[self
creatButton];

/**

* ///////////////////////////

*/

[self.window
addSubview:mainView];

[mainView release];

self.window.backgroundColor = [UIColor
whiteColor];

[self.window
makeKeyAndVisible];

return
YES;

}

/**

* /////////////////////////////////////////////

*/

– (void)creatButton{

UIButton *regisButton = [UIButton
buttonWithType:UIButtonTypeSystem];

regisButton.frame =
CGRectMake(50,
400, 220, 40);

[regisButton setTitle:@”註冊”
forState:UIControlStateNormal];

regisButton.backgroundColor = [UIColor
lightGrayColor];

[regisButton addTarget:self
action:@selector(registerClick)
forControlEvents:UIControlEventTouchUpInside];

[_loginView addSubview: regisButton];

// [regisButton release];

UIButton *loginButton = [UIButton
buttonWithType:UIButtonTypeSystem];

loginButton.frame =
CGRectMake(50,
300, 220, 40);

[loginButton addTarget:self
action:@selector(loginClick)
forControlEvents:UIControlEventTouchUpInside];

[loginButton setTitle:@”登錄”
forState:UIControlStateNormal];

[loginButton setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];

loginButton.backgroundColor = [UIColor
redColor];

loginButton.layer.cornerRadius =
5;

[_loginView addSubview:loginButton];

// [cancleButton release];

UIButton *huntButton = [UIButton
buttonWithType:UIButtonTypeSystem];

huntButton.frame =
CGRectMake(50,
200, 220, 40);

[huntButton addTarget:self
action:@selector(huntForClick)
forControlEvents:UIControlEventTouchUpInside];

[huntButton setTitle:@”找回密碼” forState:UIControlStateNormal];

[huntButton setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];

huntButton.backgroundColor = [UIColor
lightGrayColor];

huntButton.layer.cornerRadius =
5;

[_loginView addSubview:huntButton];

}

/**

* //////////////////////////////////////////////////////////

*

* @param application ;

*/

– (void)creatButton1{

UIButton *regisButton = [UIButton
buttonWithType:UIButtonTypeSystem];

regisButton.frame =
CGRectMake(50,
400, 220, 40);

[regisButton setTitle:@”註冊”
forState:UIControlStateNormal];

regisButton.backgroundColor = [UIColor
lightGrayColor];

[regisButton addTarget:self
action:@selector(registerClick1)
forControlEvents:UIControlEventTouchUpInside];

[_registerView
addSubview: regisButton];

// [regisButton release];

UIButton *cancleButton = [UIButton
buttonWithType:UIButtonTypeSystem];

cancleButton.frame =
CGRectMake(50,
300, 220, 40);

[cancleButton addTarget:self
action:@selector(cancelClick1)
forControlEvents:UIControlEventTouchUpInside];

[cancleButton setTitle:@”取消”
forState:UIControlStateNormal];

[cancleButton setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];

cancleButton.backgroundColor = [UIColor
lightGrayColor];

cancleButton.layer.cornerRadius =
5;

[_registerView
addSubview:cancleButton];

// [cancleButton release];

}

/**

* ////////////////////////////////////////////////////////////////

*/

– (void)creatButton2{

UIButton *regisButton = [UIButton
buttonWithType:UIButtonTypeSystem];

regisButton.frame =
CGRectMake(50,
400, 220, 40);

[regisButton setTitle:@”找回”
forState:UIControlStateNormal];

regisButton.backgroundColor = [UIColor
lightGrayColor];

[regisButton addTarget:self
action:@selector(zhaoHui2)
forControlEvents:UIControlEventTouchUpInside];

[_huntforView
addSubview: regisButton];

// [regisButton release];

UIButton *cancleButton = [UIButton
buttonWithType:UIButtonTypeSystem];

cancleButton.frame =
CGRectMake(50,
300, 220, 40);

[cancleButton addTarget:self
action:@selector(quXiao2)
forControlEvents:UIControlEventTouchUpInside];

[cancleButton setTitle:@”取消”
forState:UIControlStateNormal];

[cancleButton setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];

cancleButton.backgroundColor = [UIColor
lightGrayColor];

cancleButton.layer.cornerRadius =
5;

[_huntforView
addSubview:cancleButton];

// [cancleButton release];

}

// //////////////////////////////// //00000000000

– (void)loginClick{
//登錄

NSLog(@”正在登錄…”);

}

– (void)huntForClick{
//找回密碼

NSLog(@”找回密碼”);

[mainView
exchangeSubviewAtIndex:2
withSubviewAtIndex:0];

}

– (void)registerClick{
//註冊

[mainView
exchangeSubviewAtIndex:2
withSubviewAtIndex:1];

NSLog(@”註冊”);

// [mainView exchangeSubviewAtIndex:2 withSubviewAtIndex:1];

}

// ////////////////////////////// 1111111111

– (void)registerClick1{
// 註冊

}

– (void)cancelClick1{
//取消

[mainView
exchangeSubviewAtIndex:2
withSubviewAtIndex:1];

}

// ////////////////////// 22222222222

– (void)zhaoHui2{
//找回

}

– (void)quXiao2{
//取消

[mainView
exchangeSubviewAtIndex:0
withSubviewAtIndex: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.window
release];

[super dealloc];

}

@end

發佈留言