由於UICatalog工程比較大,所以我分幾篇來描述。
如果有關代碼前面已經分析過,這裡就不再重複了。
4.ControlsViewController類
這個類的.h檔案沒有什麼特殊的。
在.m檔案中,注意下面的定義:
#define kSliderHeight7.0
#define kProgressIndicatorSize40.0
#define kUIProgressBarWidth160.0
#define kUIProgressBarHeight24.0
#define kViewTag1
這樣的定義是必須,在編程規範中,原則上是不允許硬式編碼,所有的常量都應該是使用宏定義的方式。如果是多個檔案中都使用的常量,應該使用一個專門的標頭檔定義,如果只是在一個類中使用,就定義在這個類的.m檔案中。
@interface ControlsViewController (forwardDeclarations)
- (void)tintAction:(id)sender;
@end
這裡使用catalog技術對類進行擴充,這裡不對catalog技術進行講述,如果有可能,我會專門寫一個Objective-C的教程。
值得一提的是,上面的方式看似定義了類的一個私人的函數,其實不然,由於Objective-C的運行期特性,這個語言的類是沒有私人函數的概念的。如果在外面調用這個函數,編譯期會給出一個警告,但是在啟動並執行時候,程式不會crash。
4.1函數viewDidLoad
if ([UIStepperclass])
{
[self.dataSourceArrayaddObject:[NSDictionarydictionaryWithObjectsAndKeys:
@"UIStepper",kSectionTitleKey,
@"Stepper 1 to 10",kLabelKey,
@"ControlsViewController.m:\r-(UIStepper *)stepper",kSourceKey,
self.stepper,kViewKey,
nil]];
}
這個代碼是中的[UIStepper class]是判斷是否有UIStepper這個類。由於UIStepper是iOS5.0中引入的,所以這裡必須進行判斷一下,注意,即使如此,這個工程在5.0以前的模擬器中也是不能啟動並執行。
// provide tint coloring only if its available
if ([sliderCtl
respondsToSelector:@selector(minimumTrackTintColor)])
{
// add tint bar button
UIBarButtonItem *tintButton = [[UIBarButtonItemalloc]initWithTitle:@"Tinted"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(tintAction:)];
self.navigationItem.rightBarButtonItem
= tintButton;
[tintButton
release];
}
這裡的[sliderCtl respondsToSelector:@selector(minimumTrackTintColor)]是判斷類UISlider是否有函數minimumTrackTintColor,通用,這個函數也是5.0中引入的。
代碼
CGRect frame =CGRectMake(198.0, 12.0, 94.0, 27.0);
switchCtl = [[UISwitchalloc]initWithFrame:frame];
[switchCtladdTarget:selfaction:@selector(switchAction:)forControlEvents:UIControlEventValueChanged];
// in case the parent view draws with a custom color or gradient, use a transparent color
switchCtl.backgroundColor = [UIColorclearColor];
[switchCtlsetAccessibilityLabel:NSLocalizedString(@"StandardSwitch",@"")];
switchCtl.tag =kViewTag;
是產生一個UISwitch的執行個體,函數switchAction:是這個執行個體的在事件UIControlEventValueChanged的響應函數。
CGRect frame =CGRectMake(174.0, 12.0, 120.0,kSliderHeight);
sliderCtl = [[UISlideralloc]initWithFrame:frame];
[sliderCtladdTarget:selfaction:@selector(sliderAction:)forControlEvents:UIControlEventValueChanged];
// in case the parent view draws with a custom color or gradient, use a transparent color
sliderCtl.backgroundColor = [UIColorclearColor];
sliderCtl.minimumValue = 0.0;
sliderCtl.maximumValue = 100.0;
sliderCtl.continuous =YES;
sliderCtl.value = 50.0;
// Add an accessibility label that describes the slider.
[sliderCtlsetAccessibilityLabel:NSLocalizedString(@"StandardSlider",@"")];
sliderCtl.tag =kViewTag;
是產生一個UISlider的執行個體,
下面的代碼就不分析了,因為都是比較簡單的。
這裡需要提醒的是,在產生的UIActivityIndicatorView執行個體後,必須調用函數startAnimating,不然這個指示控制項,是不會開始轉動的。
5.TextFieldController類
這個類裡面基本上沒有什麼需要特別解釋的,
需要提醒的是在函數textFieldLeftView中,
UIImageView *image = [[UIImageViewalloc]
initWithImage:[UIImageimageNamed:@"segment_check.png"]];
textFieldLeftView.leftView = image;
[image
release];
textFieldLeftView.leftViewMode =UITextFieldViewModeAlways;
為text field加入一個位於左邊的view,是顯示一個圖片,當然也可以顯示任何其他的圖片。