用代碼創建處Segmentarray控件,並設置它的一些屬性:
[cpp] /*segmentarray.h*/
#import <UIKit/UIKit.h>
@interface segmentarray : UIViewController
{
//創建控件對象
UISegmentedControl *segment;
}
@property (nonatomic,retain) UISegmentedControl *segment;
@end
/*segmentarray.h*/
#import <UIKit/UIKit.h>
@interface segmentarray : UIViewController
{
//創建控件對象
UISegmentedControl *segment;
}
@property (nonatomic,retain) UISegmentedControl *segment;
@end
在 .m 文件中初始化及設置屬性值
[cpp] - (void)viewDidLoad
{
[super viewDidLoad];
//建立一個數組,來存放title
NSArray *Segmentarray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",nil];
//初始化SegmentControl對象,並把title添加進去
self.segment = [[UISegmentedControl alloc]initWithItems:Segmentarray];
//使用完後,釋放NSArray對象
[Segmentarray release];
//定位,設置添加到界面中的位置和它的寬/高
self.segment.frame = CGRectMake(40, 60, 120, 40);
//設置控件SegmentControl的風格
self.segment.segmentedControlStyle = UISegmentedControlStyleBezeled;
/* 四種控件風格
UISegmentedControlStylePlain, // large plain
UISegmentedControlStyleBordered, // large bordered
UISegmentedControlStyleBar, // small button/nav bar style. tintable
UISegmentedControlStyleBezeled, // large bezeled style. tintable
*/
//設置初始的默認值,根據索引值來設置
self.segment.selectedSegmentIndex = 2;
//把控件添加到view中
[self.view addSubview:self.segment];
//釋放SegmentControl對象
[segment release];
}
摘自 like7xiaoben