02a- Segment4Way

  • warning: Declaration of views_handler_argument::init(&$view, &$options) should be compatible with views_handler::init(&$view, $options) in /home/xnvgu7/public_html/sites/all/modules/views/handlers/views_handler_argument.inc on line 745.
  • warning: Declaration of views_handler_filter::options_validate(&$form, &$form_state) should be compatible with views_handler::options_validate($form, &$form_state) in /home/xnvgu7/public_html/sites/all/modules/views/handlers/views_handler_filter.inc on line 585.
  • warning: Declaration of views_handler_filter::options_submit(&$form, &$form_state) should be compatible with views_handler::options_submit($form, &$form_state) in /home/xnvgu7/public_html/sites/all/modules/views/handlers/views_handler_filter.inc on line 585.
  • warning: Declaration of views_handler_filter_boolean_operator::value_validate(&$form, &$form_state) should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/xnvgu7/public_html/sites/all/modules/views/handlers/views_handler_filter_boolean_operator.inc on line 149.
  • warning: Declaration of views_plugin_style_default::options(&$options) should be compatible with views_object::options() in /home/xnvgu7/public_html/sites/all/modules/views/plugins/views_plugin_style_default.inc on line 25.
  • warning: Declaration of views_plugin_row::options_validate($form, &$form_state) should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/xnvgu7/public_html/sites/all/modules/views/plugins/views_plugin_row.inc on line 135.
  • warning: Declaration of views_plugin_row::options_submit($form, &$form_state) should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/xnvgu7/public_html/sites/all/modules/views/plugins/views_plugin_row.inc on line 135.

iPhone SDK開發範例大全第三章之五Segment4Way(5/7):2009年11月07日星期六

iPhone SDK開發範例大全即iPhone Developer's CookBook的中文譯本,程式可由erica網站下載。第三章講View Controller,程式共有七個。

本文講第3個程式02a- Segment4Way,CookBook 中文譯本3-5、125~126頁(訣竅3-2):

(A) 這程式主要是做一個Navigation Bar,上面有四個Button,分別為One、 Two、 Three、 Four,click Button會顯示相對的數目,如下圖。

(B)loadView中:

17行定出applicationFrame是個UITextViewUITextViewinherit自 UIScrollView : UIView : UIResponder : NSObject,並alignmentCenter(18行)、用Georgia、size80的font。

28行~36行定義UISegmentedControl是重點。UISegmentedControlinherit自UIControl : UIView : UIResponder : NSObject

  1. 28行initWithItems的parameter是NSArray,return value是一個UISegmentedControl object。
  2. 29行momentary設定為YES時,每一個Segment並不顯示selected state,並且在點選 selectedSegment完成後也不更改selectedSegment(這一點很重要,否則不知是One、Two、Three、Four中那一個被點過了)。
  3. 30行將第一個UITextView設為空白。
  4. 31行autoresizingMaskUIView的property,此時定為UIViewAutoresizingFlexibleWidth。表示跟著畫面大小自動更改width。
  5. 32行segmentedControlStyleUISegmentedControlStyleBar,可試試其他的style。
  6. 33行設定其frame大小為400x30。
  7. 34行addTarget會將message送給Target receiver的action--segmentAction程式,當UIControlEventUIControlEventValueChanged。也就是 selectedSegment值變了。
  8. 36行,將segmentedControl放到titleView。

(C)在43~53行:segmentAction裡,根據SelectedSegmentIndex的值,用UITextView的setText(47~50行)來設定相對的數目,SelectedSegmentIndex default -1,選不同的segment會有從0開始的值。

59~64行則是設定RootViewController(61行)。

(D)本節最重要的UISegmentedControl