在 iOS 開發中,登入、註冊、找回密碼這幾個界面往往結構相似,如果每個界面都從零開始手動排版,程式碼會變得冗長又難以維護。本篇就帶大家用「封裝」的思維,把 UILabel 與 UITextField 組合成一個可重複使用的自訂元件,再透過視圖之間的切換來完成三個界面的互動。
我們先建立一個類別,把「左邊的說明文字(Label)」與「右邊的輸入框(TextField)」打包在一起,讓外部只需要傳入文字就能快速產生一列輸入欄位。
以下是 TLView.m 的實作:
#import "TLView.h"
@interface TLView ()
{
UILabel *_desLabel; //左側說明文字
UITextField *_textField;//右側輸入框
}
@end
@implementation TLView
//重寫父類的初始化方法,處理所有元件共同的排版
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
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;
[self addSubview:_desLabel];
[_desLabel release];
//建立 UITextField
_textField = [[UITextField alloc] initWithFrame:CGRectMake(0.4 * width, 0, 0.6 * width, height)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_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) {
_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
有了可重複使用的元件後,我們在應用代理裡建立三個 UIView,分別代表登入、註冊與找回密碼界面,並透過 exchangeSubviewAtIndex:withSubviewAtIndex: 交換視圖的層級順序,達到「切換界面」的效果。
#import "ZKJAppDelegate.h"
#import "TLView.h"
#define main_tag 1000;
@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]];
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) { [ltView setSecureEntry:YES]; }
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 addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
[_loginView addSubview:regisButton];
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.backgroundColor = [UIColor redColor];
loginButton.layer.cornerRadius = 5;
[_loginView addSubview:loginButton];
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.layer.cornerRadius = 5;
[_loginView addSubview:huntButton];
}
//註冊界面的按鈕
- (void)creatButton1{
UIButton *regisButton = [UIButton buttonWithType:UIButtonTypeSystem];
regisButton.frame = CGRectMake(50, 400, 220, 40);
[regisButton setTitle:@"註冊" forState:UIControlStateNormal];
[regisButton addTarget:self action:@selector(registerClick1) forControlEvents:UIControlEventTouchUpInside];
[_registerView addSubview:regisButton];
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.layer.cornerRadius = 5;
[_registerView addSubview:cancleButton];
}
//找回密碼界面的按鈕
- (void)creatButton2{
UIButton *regisButton = [UIButton buttonWithType:UIButtonTypeSystem];
regisButton.frame = CGRectMake(50, 400, 220, 40);
[regisButton setTitle:@"找回" forState:UIControlStateNormal];
[regisButton addTarget:self action:@selector(zhaoHui2) forControlEvents:UIControlEventTouchUpInside];
[_huntforView addSubview:regisButton];
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.layer.cornerRadius = 5;
[_huntforView addSubview:cancleButton];
}
//各按鈕的點擊事件:透過交換子視圖的層級來切換界面
- (void)loginClick{ NSLog(@"正在登錄..."); }
- (void)huntForClick{ NSLog(@"找回密碼"); [mainView exchangeSubviewAtIndex:2 withSubviewAtIndex:0]; }
- (void)registerClick{ [mainView exchangeSubviewAtIndex:2 withSubviewAtIndex:1]; }
- (void)registerClick1{ }
- (void)cancelClick1{ [mainView exchangeSubviewAtIndex:2 withSubviewAtIndex:1]; }
- (void)zhaoHui2{ }
- (void)quXiao2{ [mainView exchangeSubviewAtIndex:0 withSubviewAtIndex:2]; }
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
@end
這個範例的核心思路是:把重複的「Label + TextField」結構封裝成獨立元件,再透過交換父視圖中子視圖的層級順序來達到界面切換的效果。這種元件化思維能有效降低重複程式碼,讓後續擴充欄位或調整樣式時更輕鬆。