01a-HelloAlert2

  • 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開發範例大全第四章之二01a-HelloAlert2(2/15):2009年11月11a日星期二

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

本文講第2個程式01a-HelloAlert2,CookBook 中文譯本4-1-2、141~142頁(程式碼4-1),本程式與iPhone SDK開發範例大全第四章之一00a-HelloAlert(1/15),但在第7行加上conform to UIAlertViewDelegate,注意142頁下半頁UIAlertViewDelegate說明:

(A)本程式在Naviagtion Bar上有"DO It" button,click這個button即出現一個pop up的view。這就是用Alert View做的。

(B)達成pop up Alert View 的方法很簡單,下51行action:@selector(presentSheet)讓使用者click DoIt時跳至第24行的presentSheet。詳述presentSheet如下:

26行:UIAlertView *baseAlert = [[UIAlertView alloc ] .....產生UIAlertView 物件。

26行~29行:

  1. 27行~initWithTitle : (NSString *) title讓title出現在receiver的title bar。receiver是一個UIAlertView 物件
  2. 27行~message: (NSString *)message讓title下有更多說明,@""表示沒有。
  3. 28行~delegate:(id) delegate,設立receiver的delegate。nil表示沒有,self表示receiver即為HelloController自己。
  4. 28行~cancelButtonTitle:(NSString *) cancelButtonTitle,cancel button的title,nil表示沒有cancel button。本message相當於addButtonWithTitle:。
  5. 29行~otherButtonTitles:(NSString *)otherButtonTitles, ...,....表示可以有一串nil ended的(NSString *) ;所以,可以是otherButtonTitles:@"Button 1", @"Button 2", .... nil。 本message相當於addButtonWithTitle:。
  6. return value:一個initialize好的Alert View。

30行、[baseAlert show] : 用animation來顯示receiver。Alert View是以modal view方式顯現。

同時,因conform to UIAlertViewDelegate,加了15~19行這clickedButtonAtIndex protocal的message,當使用者click OK button時會跳入此程式。說明如下。

這個程式只是在17行printf出clicked button的button index + 1,因為只有OKbutton,而button index從0開始,所以在gdb中顯示出:

User Pressed Button 1

18行[alertView release] release alertView。

receiver自動dismiss。這也是為何line 4要有dismiss。