本文示範如何在同一個畫面中,透過交換子檢視的層級,實作登錄、註冊與找回密碼三個界面之間的切換,並以封裝好的 Label 與 TextField 元件快速搭建表單。
把「左邊說明 Label + 右邊輸入框」這類重複的表單列抽成一個可重複使用的元件,並透過客製初始化方法處理差異部分:
#import "TLView.h"
@interface TLView ()
{
UILabel *_desLabel; // 左邊的 Label
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;
_desLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0.3 * width, height)];
_desLabel.font = [UIFont systemFontOfSize:18];
_desLabel.textAlignment = NSTextAlignmentRight;
[self addSubview:_desLabel];
[_desLabel release];
_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;
}
// 對外開放各種設定方法
- (void)setSecureEntry:(BOOL)secureEntry { _textField.secureTextEntry = secureEntry; }
- (void)setKeyBoardType:(UIKeyboardType)keyBoardType { _textField.keyboardType = keyBoardType; }
- (void)setDelegate:(id <UITextFieldDelegate>)delegate { _textField.delegate = delegate; }
- (NSString *)text { return _textField.text; }
@end
建立登錄、註冊、找回密碼三個 view,全部加到 mainView 上,透過 exchangeSubviewAtIndex:withSubviewAtIndex: 交換層級來達到「切換」的效果:
#define main_tag 1000
#define zhuce_tag 1001
#define youxiang_tag 1002
- (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];
// 第二張:註冊
_registerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
_registerView.backgroundColor = [UIColor whiteColor];
[mainView addSubview:_registerView];
[_registerView release];
// 用迴圈快速產生註冊表單的每一列
NSArray *lableTexts = @[@"用戶:", @"密碼:", @"確認密碼:", @"手機號:", @"郵箱:"];
NSArray *placeholders = @[@"請輸入用戶:", @"請輸入密碼:", @"請輸入確認密碼:", @"請輸入手機號:", @"請輸入郵箱:"];
for (int i = 0; i <= 4; i++) {
TLView *ltView = [[TLView alloc] initWithFrame:CGRectMake(30, 50 * i + 50, 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;
[_registerView addSubview:ltView];
}
// 第一張:登錄(最上層)
_loginView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
_loginView.backgroundColor = [UIColor lightGrayColor];
_loginView.tag = main_tag;
[mainView addSubview:_loginView];
[_loginView release];
[self.window addSubview:mainView];
[mainView release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
切換的核心就是交換子檢視的層級索引,把目標界面換到最上層:
- (void)registerClick { // 點「註冊」:把註冊界面換到最上
[mainView exchangeSubviewAtIndex:2 withSubviewAtIndex:1];
}
- (void)huntForClick { // 點「找回密碼」:把找回密碼界面換到最上
[mainView exchangeSubviewAtIndex:2 withSubviewAtIndex:0];
}
- (void)cancelClick1 { // 註冊界面的「取消」:換回登錄
[mainView exchangeSubviewAtIndex:2 withSubviewAtIndex:1];
}
- (void)quXiao2 { // 找回密碼界面的「取消」:換回登錄
[mainView exchangeSubviewAtIndex:0 withSubviewAtIndex:2];
}
整個方案的精髓在於兩點:一是把重複的「Label + TextField」表單列封裝成 TLView,用迴圈與陣列快速量產;二是用 exchangeSubviewAtIndex 交換 view 的層級來模擬界面切換,無需真的建立多個 ViewController,就能在同一畫面內流暢切換登錄、註冊與找回密碼三種狀態。