Segment Control Using Storyboard in iPhone or iPad

Introduction

In this article I will create a Single View application. Here I use a button, Segment control from outlet and write a code for implementing a new segment control that is shown when we click on the Segment control button. One more thing here; we implement all processed on ios new feature storyboard.

To understand it we use the following procedure.

Step 1

Open XCode by double-clicking on it.

Step 2

Create a New XCode Project by clicking on it.

Step 3

Now Select Single View Application and click on Next.

Step 4

Now provide your Product Name and Company Identifier.

Step 5

Select the location where you want to save your project and click on Create.

Step 6 

Now here we write Code.

 AppDelegate.h

//
// AppDelegate.h
// test
//
// Created by Sachin Bhardwaj on 29/12/12.
// Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

 
 AppDelegate.m

//
// AppDelegate.m
// test
//
// Created by Sachin Bhardwaj on 29/12/12.
// Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end


 ViewController.h

//
// ViewController.h
// test
//
// Created by Sachin Bhardwaj on 29/12/12.
// Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
- (IBAction)colorSelected:(UISegmentedControl *)sender;
- (IBAction)SegementedControlPressed:(UIButton *)sender;
@end

 ViewController.m

//
// ViewController.m
// test
//
// Created by Sachin Bhardwaj on 29/12/12.
// Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
- (IBAction)colorSelected:(UISegmentedControl *)sender
{
switch (sender.selectedSegmentIndex) {
case 0:
self.view.backgroundColor = [UIColor whiteColor];
break;
case 1:
self.view.backgroundColor = [UIColor redColor];
break;
case 2:
self.view.backgroundColor = [UIColor greenColor];
break;
case 3:
self.view.backgroundColor = [UIColor blueColor];
break;
default:
break;
}
}

- (void)mySegPressed:(UISegmentedControl *)sender
{
NSLog(@"%d", sender.selectedSegmentIndex);
}

- (IBAction)SegementedControlPressed:(UIButton *)sender
{
UISegmentedControl __autoreleasing *mySegControl
= [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:@"Happy", @"New", @"Year", @"2013", nil]];
CGRect segFrame = CGRectMake(20, self.view.frame.size.height / 2 - 15,
self.view.frame.size.width - 40, 30);
[mySegControl addTarget:self action:@selector(mySegPressed:) forControlEvents:UIControlEventValueChanged];
mySegControl.frame = segFrame;
[self.view addSubview:mySegControl];
sender.hidden = YES;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

 ViewController.Xib

viewController.xib-in-iphone.jpg 

viewController.xib-in-ipad.jpg

Step 7

Click on the Run button to show the output.

Output1 in iPhone:

output1-in-iPhone.jpg

Output2 in iPhone:

output2-in-iphone.jpg

Output3 in iPhone:

output3-in-iPhone.jpg

Output4 in iPhone:

output4-in-iPhone.jpg

When we click on the Segment Control button it will be disappear and the new segment which is developed by code appears in the iPhone screen.

Output5 in iPhone

When we click on the first segment button (Happy) it shows output in the xcode output window, as in:

output5-in-iPhone.jpg

 
When we click on the last segment button (2013) it shows output in the xcode output window, as in:

output-in-xcode.jpg

Output1 in iPad:

output1-in-iPad.jpg

Output2 in iPad:

output2-in-iPad.jpg

Output3 in iPad:

output3-in-iPad.jpg

Output4 in iPad:

output4-in-iPad.jpg

Output5 in iPad:

output5-in-iPad.jpg

Wish you a very Happy New Year 2013.

Cheers!!


Similar Articles