Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 435)];
- scroll.contentSize = CGSizeMake(320, 700);
- scroll.showsHorizontalScrollIndicator = YES;
- NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
- UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
- segmentedControl.frame = CGRectMake(35, 200, 250, 50);
- segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
- [segmentedControl addTarget:self action:@selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];
- segmentedControl.selectedSegmentIndex = 1;
- [scroll addSubview:segmentedControl];
- [segmentedControl release];
- [self.view addSubview:scroll];
- - (void)MySegmentControlAction:(UISegmentedControl *)segment
- {
- if(segment.selectedSegmentIndex == 0)
- {
- // code for the first button
- }
- }
- UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithItems:@[@"One",@"Two"]];
- [segmentControl setSegmentedControlStyle:UISegmentedControlStyleBar];
- segmentControl.frame = CGRectMake(10, 50, 300, 30);
- [segmentControl addTarget:self action:@selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged];
- [segmentControl setSelectedSegmentIndex:0];
- [scrollView addSubview:segmentControl];
- [segmentControl release];
- -(void)segmentedControlValueDidChange:(UISegmentedControl *)segment
- {
- switch (segment.selectedSegmentIndex) {
- case 0:{
- //action for the first button (Current)
- break;}
- case 1:{
- //action for the first button (Current)
- break;}
- }
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common-bg.jpg"]];
- [self.navigationItem setHidesBackButton:YES];
- //-- For creating segment control in navigation bar
- UISegmentedControl *mainSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Back", @"Month", @"Year", @"Home", nil]];
- [mainSegment setSegmentedControlStyle:UISegmentedControlStyleBar];
- mainSegment.frame = CGRectMake(0,0, 400, 43);
- self.navigationItem.titleView = mainSegment;
- mainSegment.selectedSegmentIndex = 1;
- [mainSegment addTarget:self action:@selector(mainSegmentControl:) forControlEvents: UIControlEventValueChanged];
- [self.view addSubview:mainSegment];
- //--**--
- }
- - (void)mainSegmentControl:(UISegmentedControl *)segment
- {
- if(segment.selectedSegmentIndex == 0)
- {
- // action for the first button (Current or Default)
- }
- else if(segment.selectedSegmentIndex == 1)
- {
- // action for the second button
- }
- else if(segment.selectedSegmentIndex == 2)
- {
- // action for the third button
- }
- else if(segment.selectedSegmentIndex == 3)
- {
- // action for the fourth button
- }
- }
- NSArray *itemArray = [NSArray arrayWithObjects: @"Uno", @"Dos", @"Tres", nil];
- UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
- segmentedControl.frame = CGRectMake(35, 200, 250, 50);
- [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents: UIControlEventValueChanged];
- segmentedControl.selectedSegmentIndex = 1;
- [self.view addSubview:segmentedControl];
- - (void)segmentAction:(UISegmentedControl *)segment
- {
- switch (segment.selectedSegmentIndex) {
- case 0:
- // Uno
- break;
- case 1:
- // Dos
- break;
- case 2:
- // Tres
- break;
- default:
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement