iPhone SDK開發範例大全第五章之二FontPickerTable(2/10)

  • 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開發範例大全第五章之二FontPickerTable(2/10):2010年05月31日星期一

iPhone SDK開發範例大全即iPhone Developer's CookBook的中文譯本,程式可由erica網站下載。第五章講基本表格,程式共有十個。這是第二個程式---02a-FontPickerTable。

本程式練習將一個table自頂端滑出(點圖一右上角<Do It>),有三種font可以選擇,選完,點選<Okay>,再點圖一左上角<Check>,顯示出剛選擇的font,在iPhone simulator執行結果如圖一,程式列於圖二,原程式為OS2.0版,在OS3.0時,cell.text己無法使用了,故將第44行改為45行。程式解說如下:

本程式(圖二)有三大部份,一為第3行 sysBlueColor回覆UIColor,第16行、23行的FontPickerSheet(是個UIView,interface UITABleViewDataSource及UITableViewDelegate),一為第162、168行的HelloController,一為第234、241行的SampleAppAppDelegate,一為第257行的main。在進入詳細說明前,會有個疑問,為何table一開始並未出現?這是因為圖二程式221行[alertView setCenter:CGPointMake(160.0f, -240.0f];將table中心設在y軸-240,因而table在status bar之上。若將這行去掉,table仍未出現,原因是64行rect.origin.y = 0.0f - rect.size.height;將 status bar放在status bar的 0 - rect.size.height之上,因而table全部看不見了。試著先將221行改為[alertView setCenter:CGPointMake(160.0f, 120.0f];,table就出現了。或是將221行omment掉並將64行改為rect.origin.y = 0.0f - rect.size.height + 240.0f;,table也就出現了。

第253行的main:其作用只是程式的entry point。在260行,由SampleAppDelegate跳入主程式jTableAppDelegate(第218行)。

第234、241行的SampleAppDelegate:其作用是有個applicationDidFinishLaunching(第243行)為entry point,main一完就自動跳入此處。在其中產生一個window(第244行),產生一個是Navaigation的view(第245行 UINavigationView Controller),並將HelloController設為其Controller,由此跳入HelloController,在第246行將NavigationView Controller這個UIView加入window subview(此時成為HelloController view)。

第162、168行的HelloController:

169行init,SampleAppDelegate內245行用到此init;

在SampleAppDelegate內applicationDidFinishLaunching(第243行)執行完即跳入199行loadView,201行產生contentView,207行加入rightBarButtonItem "Do It" 、action為presentSheet(175行); 214 行加入leftBarButtonItem "Check" 、action為doNotify(180行);

220行產生aletView。

[[FontPickerSheet alloc] initWithFrame:...] 會跳入FontPickerSheet內的initWithFrame

221行[alertView setCenter:CGPointMake(160.0f, -240.0f];將table中心設在y軸-240,因而table在status bar之上。若將這行去掉,table仍未出現,原因是64行rect.origin.y = 0.0f - rect.size.height;將 status bar放在status bar的 0 - rect.size.height之上,因而table全部看不見了。試著先將221行改為[alertView setCenter:CGPointMake(160.0f, 120.0f];,table就出現了。或是將221行omment掉並將64行改為rect.origin.y = 0.0f - rect.size.height + 240.0f;,table也就出現了。

175行的presentSheet只是在需alertView時[alertView presentView];

180行的doNitify:182行設好message;189行產生UIAlertView alert; 195行顯示alert。

第16行的FontPickerSheet:共分四部份,分別是第18行的init、第24行的Regular methods、第 79 行的Table View Methods( UITableViewDataSource Methods)、第115行的UITableViewDelegateMethods。

30~56行為tableView DataSource及AppDelegate methods。

第62行的initWithFrame:64~67行設frame的origin、alpha、backgroundcolor;72~80行設Okay button;84~91行設table title;94~98行設table的border;101~108行設設table本身。

64行rect.origin.y = 0.0f - rect.size.height;將 status bar放在status bar的 0 - rect.size.height之上,因而table全部看不見了,這就是隱藏table的方式。若此處不隱藏,221行仍會將table隱藏在status bar之上。

第136行的presentView是由175行的presentSheet內177行的[alertView presentView];call到:

141~144行用CGContectRef滑入一個UIView。

這個view即為自己這個frame(146行)。

第117行removeView:

119~120行拿到62行的initWithFrame內101行的tableView;123~133用CGContectRef滑出一個UIView。

 

圖一:執行結果

 

 

圖二:執行結果