用代碼建立處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