I have set up a segmented control in my project. I want to use it in my settings scene to control whether the orientation is landscape left or landscape right. I have in set up in my storyboard and here is my code in my SettingsViewController.h
#import <UIKit/UIKit.h>
@interface SettingsViewController : UIViewController
{
IBOutlet UISegmentedControl *orientation;
}
@property (nonatomic, retain) UISegmentedControl *orientation;
- (IBAction) setOrientation;
@end
and here is my code in SettingsViewController.m
#import "SettingsViewController.h"
@implementation SettingsViewController
@synthesize orientation;
- (IBAction)setOrientation
{
NSLog(@"index = %d\n", orientation.selectedSegmentIndex);
if (orientation.selectedSegmentIndex == 0) {
NSLog(@"left\n");
}
if (orientation.selectedSegmentIndex == 1) {
NSLog(@"right\n");
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
@end
I was wondering why the value isn’t changing from 0 to 1 when I select the right side of the control. Also I was wondering if in my .h I need to declare IBOutlet because I dont use the segmented control as an outlet. I just use it to take in which side is selected and then use that to set the orientation of the app.
Try this. Hook it up in the Storyboard to the segmentedControl using valueChanged.