Segmented Controller
Segmented Controller最典型的應用就是在行事曆的日、月、週切換,呈現的資料會根據日月週來分別呈現,下面使用Segmented Controller來切換網頁呈現網站內容。
以下使用3個Segments來做Google、Yahoo、Youtube的切換。
**Step 1.**建立SingleView專案,專案命名為Segmented Contrller。
**Step 2.**編輯MainStoryboard.storyboard。
該專案會使用到的元件有:
ToolbarWeb ViewTextFieldSegmented ControllerActivity Indicator View
Activity Indicator View的設定如同之前Web View一樣。
Segmented Controlle在預設中只有兩個選項,要增加選項可在Attributes inspector的Segments設定成你要的數量,接著在下方Segment可以設定Segment 0的Title,根據你選擇到第幾個Segment可以個別去設定Title。
**Step 3.**編輯ViewController.h。
定義的變數如同Web View內的,這裡多一個UISegmentedControl,hitSegmented是Segmented Controller的觸發事件。
1 | @interface ViewController : UIViewController<UIWebViewDelegate> |
**Step 4.**編輯ViewController.m。
載入網頁的過程也如同Web View的說明,因此在viewDidLoad、hitReturn方法都與Web View相同;UIActivityIndicatorView的操作也一樣僅需將Web View中的webViewDidStartLoad、webViewDidFinishLoad複製過來即可。
這裡則是多了一個hitSegmented的方法,載入網頁的過程都是使用相同的方法。
1 | -(IBAction)hitSegmented:(id)sender |
**Step 5.**建立關連。