1

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.

4

2 回答 2

0

尝试这个。使用 valueChanged 在 Storyboard 中将其连接到 segmentedControl。

- (IBAction)segmentedControlChanged:(id)sender {
    UISegmentedControl *segmentedC = (UISegmentedControl *)sender;

    if (segmentedC.selectedSegmentIndex == 0) {
//      NSLog(@"selectedSegmentIndex == 0");
        [self selectFirstSegment];
    }
}
于 2012-08-10T19:05:58.550 回答
0

在段动作中设置方向景观或potriat ..并在.m文件中设置布尔方向类型方法集的结尾

return yes; 

在所有 .m 文件中。

于 2012-08-10T19:07:54.377 回答