2012-08-10 5 views
1

내 프로젝트에서 세그먼트 컨트롤을 설정했습니다. 내 설정 장면에서 방향을 사용하여 왼쪽 또는 오른쪽으로 가로 방향을 제어하고 싶습니다.UISegmentedControl에서 값을 변경하는 방법?

#import <UIKit/UIKit.h> 

@interface SettingsViewController : UIViewController 
{ 
    IBOutlet UISegmentedControl *orientation; 
} 

@property (nonatomic, retain) UISegmentedControl *orientation; 

- (IBAction) setOrientation; 

@end 

여기가 SettingsViewController.m 내 코드 내 스토리 보드에서 설정에 있고 여기에 내 코드 내 SettingsViewController.h에

#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 

값이없는 이유를 궁금 해서요 컨트롤의 오른쪽을 선택하면 0에서 1로 변경됩니다. 또한 나는 내 .h에서 IBOutlet을 선언해야하는지에 대해 궁금해했다. 왜냐하면 분할 된 컨트롤을 콘센트로 사용하지 않기 때문이다. 방금 선택한면을 선택한 다음 앱의 방향을 설정하는 데 사용합니다.

답변

0

시도해보십시오. Storyboard에서 valueChanged를 사용하여 segmentedControl에 연결합니다. 부울 방향 입력 방법의하는 .m 파일 끝의 방향 가로 또는 potriat..and을 설정 세그먼트 행동

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

    if (segmentedC.selectedSegmentIndex == 0) { 
//  NSLog(@"selectedSegmentIndex == 0"); 
     [self selectFirstSegment]; 
    } 
} 
+1

감사합니다. 그것은 작동한다 :) (나는 그것을 투표 할 것이나 나는 그것을하기에 충분한 충분한 담당자가 없다 : /) – sbru

0

모든하는 .m 파일에

return yes; 

을 설정합니다.

관련 문제