寫 iOS 介面,最終都會回到幾個基礎控件上。本文整理 Button、開關、滑桿、分段控件、工具列與 WebView 這六個最常見的 UIKit 元件,包含各自的關鍵 API、屬性設定方式與最小可運行的範例。
UIButton 之所以能做出很有層次的視覺效果,關鍵在於它支援多種狀態,每種狀態都能獨立設定圖片、背景與標題:
· Default State(預設)
· Highlighted State(按下時)
· Selected State(選取)
· Disabled State(不可用)


以下實作「點擊 Disable 按鈕,在兩個按鈕的可用/不可用狀態之間切換」。先建立 ButtonsBackground 專案。
ButtonsBackgroundViewController.h:
@interface ButtonsBackgroundViewController : UIViewController {
UIButton *clearButton;
UIButton *smallButton;
}
@property (nonatomic, retain) IBOutlet UIButton *clearButton;
@property (nonatomic, retain) IBOutlet UIButton *smallButton;
- (IBAction)disableBut:(id)sender;
@end
ButtonsBackgroundViewController.m:
@synthesize clearButton;
@synthesize smallButton;
- (IBAction)disableBut:(id)sender {
if (clearButton.enabled == YES) {
clearButton.enabled = NO;
smallButton.enabled = NO;
[((UIButton *)sender) setTitle:@"Enable" forState:UIControlStateNormal];
} else {
clearButton.enabled = YES;
smallButton.enabled = YES;
[((UIButton *)sender) setTitle:@"Disable" forState:UIControlStateNormal];
}
}
- (void)dealloc {
[clearButton release];
[smallButton release];
[super dealloc];
}
點擊 Disable 時會呼叫 disableBut:,其中切換了兩個按鈕的 enabled 狀態,同時改寫自己身上的標題。這裡的 sender 就是事件源,也就是被點擊的那顆按鈕本身。
介面配置要點:把 File's Owner 連到 clearButton 與 smallButton 兩個 outlet;再把 Disable 按鈕的 Action 連到 disableBut:。
clearButton 屬性設定:
· 勾選 “Shows Touch on Highlight”
· Default State:圖片 power.png、背景 butbackgray.png
· Highlighted State:圖片 power.png、背景 butbackbluegray.png
· Disabled State:圖片 powerdisabled.png、背景 butbackgraydisabled.png
smallButton 屬性設定:
· Default State:背景 butbackgray.png、title “Shock”
· Highlighted State:背景 butbackbluegray.png、title “Shocking”
類似桌面系統的 checkbox,只有 true / false 兩種狀態。以程式碼切換狀態:
- (void)setOn:(BOOL)on animated:(BOOL)animated

水平放置,可用手拖動改變數值。預設最小值 0、最大值 1.00、初始值 0.50,可透過下列方法調整:
- (void)setValue:(float)value animated:(BOOL)animated

開關與滑桿實例

SwitchSliderViewController.h:
@interface SwitchSliderViewController : UIViewController {
UISwitch *mySwitch;
}
@property (nonatomic, retain) IBOutlet UISwitch *mySwitch;
- (IBAction)handleSwitch:(id)sender;
- (IBAction)handleSlider:(id)sender;
@end
SwitchSliderViewController.m:
@implementation SwitchSliderViewController
@synthesize mySwitch;
- (IBAction)handleSwitch:(id)sender {
if ([((UISwitch *)sender) isOn] == YES) {
NSLog(@"It's on");
} else {
NSLog(@"It's off");
}
}
- (IBAction)handleSlider:(id)sender {
NSLog(@"value: %f", ((UISlider *)sender).value);
if ([((UISlider *)sender) value] == ((UISlider *)sender).maximumValue) {
[mySwitch setOn:YES animated:YES]; // 滑桿拉到底時自動開啟開關
}
}
- (void)dealloc {
[mySwitch release];
[super dealloc];
}
連線重點:開關的 Value Changed 連到 handleSwitch:,滑桿的 Value Changed 連到 handleSlider:,並指定開關的 outlet。
由兩段或更多段組成,每段行為近似一顆獨立按鈕,常用來做同層級的分頁切換。

- (IBAction)handleSegment:(id)sender {
UISegmentedControl *mySeg = (UISegmentedControl *)sender;
if (mySeg.selectedSegmentIndex == 0) {
NSLog(@"selected zero index...");
} else if (mySeg.selectedSegmentIndex == 1) {
NSLog(@"selected one index...");
} else {
NSLog(@"selected two index...");
}
}

把段控件的 Value Changed 連到 File's Owner 的 handleSegment: 即可。
工具列通常固定在畫面底部,內部可擺放多個按鈕或控件。

@interface ToolBarViewController : UIViewController {
IBOutlet UIActivityIndicatorView *myActivityView;
}
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *myActivityView;
- (IBAction)onClickStartButton:(id)sender;
- (IBAction)onClickOpenButton:(id)sender;
@end
@implementation ToolBarViewController
@synthesize myActivityView;
- (IBAction)onClickStartButton:(id)sender {
if ([myActivityView isAnimating]) {
[myActivityView stopAnimating];
} else {
[myActivityView startAnimating];
}
}
- (IBAction)onClickOpenButton:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
message:@"您點擊了打開按鈕"
delegate:self
cancelButtonTitle:@"Done"
otherButtonTitles:nil];
[alert show];
[alert release];
}
@end

連線重點:開啟按鈕連 onClickOpenButton:、開始按鈕連 onClickStartButton:,並把 UIActivityIndicatorView 連到 outlet。
UIWebView 是打造混合型 iPhone 應用的利器,不少網站的 iOS 客戶端就是這樣做出來的。它支援 HTML5,但不支援 Flash。

@interface MyWebViewController : UIViewController <UIWebViewDelegate> {
IBOutlet UITextField *myTextField;
IBOutlet UIWebView *myWebView;
}
@property (nonatomic, retain) UIWebView *myWebView;
@property (nonatomic, retain) UITextField *myTextField;
- (IBAction)changeLocation:(id)sender;
@end
委托(Delegate)是 iOS 中極重要的設計模式,主要用於事件回呼。宣告了某個協議,就等於承諾提供其中方法的實作。
@implementation MyWebViewController
@synthesize myWebView;
@synthesize myTextField;
- (void)viewDidLoad {
myWebView.delegate = self;
}
- (void)dealloc {
myWebView.delegate = nil;
[myTextField release];
[myWebView release];
[super dealloc];
}
- (IBAction)changeLocation:(id)sender {
[myTextField resignFirstResponder];
NSURL *url = [NSURL URLWithString:myTextField.text];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];
}
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"%@", [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"]);
}
@end
其中 webViewDidFinishLoad: 在頁面載入完成後回呼;而 stringByEvaluatingJavaScriptFromString: 能同步執行一段 JavaScript 並取回結果,例如用 document.body.innerHTML 取得整頁 HTML,或用 document.documentElement.textContent 只取純文字內容。這是原生與網頁互動最簡單的橋梁。