`
icheng
  • 浏览: 831794 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
文章分类
社区版块
存档分类
最新评论

iOS5编程--官方例子代码的研究--2.UICatalog-2

 
阅读更多

由于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]];

}


这个代码是中的[UIStepperclass]是判断是否有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];

}


这里的[sliderCtlrespondsToSelector:@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,是显示一个图片,当然也可以显示任何其他的图片。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics