Use the code to create the Segmentarray control and set its attributes:
/* Segmentarray. h */# import <UIKit/UIKit. h> @ interface segmentarray: UIViewController {// create control object UISegmentedControl * segment;} @ property (nonatomic, retain) UISegmentedControl * segment; @ end
Initialize and set attribute values in the. m file
-(Void) viewdidload {[Super viewdidload]; // create an array to store the title nsarray * segmentarray = [[nsarray alloc] initwithobjects: @ "1 ", @ "2", @ "3", @ "4", nil]; // initialize the segmentcontrol object and add the title to self. segment = [[uisegmentedcontrol alloc] initwithitems: segmentarray]; // after use, release the nsarray object [segmentarray release]; // locate, set the position added to the interface and its width/height self. segment. frame = cgrectmake (40, 60,120, 40); // you can specify the segmentcontrol style self. segment. segmentedcontrolstyle = custom;/* Four control styles uisegmentedcontrolstyleplain, // large plain controls, // large bordered uisegmentedcontrolstylebar, // small button/nav bar style. tintable uisegmentedcontrolstylebezeled, // large bezeled style. tintable * // set the initial default value and Set Self according to the index value. segment. selectedsegmentindex = 2; // Add the control to view [self. view addsubview: Self. segment]; // release the segmentcontrol object [segment release];}