2017-02-10 2 views
0

시뮬레이터를 실행할 때 다른 요소의 모든 색을 반대로하면이 코드에서 색인에 잘못된 것이 무엇인지 찾을 수 없습니다.UISegmentedControl에서 모든 세그먼트의 색조 색 순서가 왜 바뀌 었습니까? 그것을 고치는 방법?

UISegmentedControl의 마지막 서브 뷰가에 남아
#import "ViewController3.h" 

@interface ViewController3() 
@property (strong, nonatomic) IBOutlet UIView *segments; 
@property (weak, nonatomic) IBOutlet UILabel *label; 

@end 

@implementation ViewController3 




- (void)viewDidLoad { 
[super viewDidLoad]; 
//creating textField programmatically 
// UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 20)]; 
// textField.borderStyle = UITextBorderStyleRoundedRect; 
// textField.font = [UIFont systemFontOfSize:15]; 
// textField.placeholder = @"enter text here"; 
// textField.autocorrectionType = UITextAutocorrectionTypeNo; 
// textField.keyboardType = UIKeyboardTypeDefault; 
// textField.returnKeyType = UIReturnKeyDone; 
// textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
// textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
// [self.view addSubview:textField]; 

//setting tint color for segments 
[[_mysegment.subviews objectAtIndex:0] setTintColor:[UIColor redColor]]; 

[[_mysegment.subviews objectAtIndex:1] setTintColor:[UIColor blueColor]]; 

[[_mysegment.subviews objectAtIndex:2] setTintColor:[UIColor greenColor]]; 

[[_mysegment.subviews objectAtIndex:3] setTintColor:[UIColor purpleColor]]; 

[[_mysegment.subviews objectAtIndex:4] setTintColor:[UIColor orangeColor]]; 

//to preload everyview before hand 
[email protected]"1"; 
_image1.image = [UIImage imageNamed:@"fruits.png"]; 
_colorToBeDisplayedInView.backgroundColor = [UIColor redColor]; 
} 

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

/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
// Get the new view controller using [segue destinationViewController]. 
// Pass the selected object to the new view controller. 
} 
*/ 


- (IBAction)segmentControl:(id)sender { 
if (_mysegment.selectedSegmentIndex==0) 
{ 
    [email protected]"1"; 
    _image1.image = [UIImage imageNamed:@"fruits.png"]; 
    _colorToBeDisplayedInView.backgroundColor = [UIColor redColor]; 
} 
else if (_mysegment.selectedSegmentIndex==1) 
{ 
    [email protected]"2"; 
    _image1.image = [UIImage imageNamed:@"airplane.png"]; 
    _colorToBeDisplayedInView.backgroundColor = [UIColor blueColor]; 
} 
else if (_mysegment.selectedSegmentIndex==2) 
{ 
    [email protected]"3"; 
    _image1.image = [UIImage imageNamed:@"boat.png"]; 
    _colorToBeDisplayedInView.backgroundColor = [UIColor greenColor]; 
} 
else if (_mysegment.selectedSegmentIndex==3) 
{ 
    [email protected]"4"; 
    _image1.image = [UIImage imageNamed:@"arctichare.png"]; 
    _colorToBeDisplayedInView.backgroundColor = [UIColor purpleColor]; 
}else 
{ 
    [email protected]"5"; 
    _image1.image = [UIImage imageNamed:@"cat.png"]; 
    _colorToBeDisplayedInView.backgroundColor = [UIColor orangeColor]; 
} 
} 
@end 
+0

그것이 이미 질문에 있었는지 확실하지 않습니다. 시뮬레이터의 스크린 샷 링크는 다음과 같습니다. https://i.stack.imgur.com/UZ9qC.png – N4SK

+0

'UISegmentaionControl'에서 두 세그먼트만으로 같은 코드를 시도한 다음, 그 코드가주는 O/P를 확인하십시오. –

+0

틴트 색상을 직접 지정했지만 일부 조건에서는 좋지 않습니다. 그래서 문자열을 기반으로 세그먼트에 대한 색상을 설정 ... – Marking

답변

0

let x = UISegmentedControl(items: ["Red", "Green", "blue"]) 
x.subviews.last?.frame //(0, 0, 51, 29) 
+0

이것은 신속한 코드이며 목적을 사용하여 메신저입니다. 이 코드의 논리를 구현하는 방법을 모르겠다. – N4SK

0
NSString *value = [yourcolorsSegmentName titleForSegmentAtIndex:yourcolorsSegmentName.selectedSegmentIndex]; 
NSLog(@"%@", value); 
if ([value isEqualToString:@"Red"]) { 
//set tint color... 
} else if ([value isEqualToString:@"Green"]) { 
//set tint color... 
} 
: 여기
#import <UIKit/UIKit.h> 

@interface ViewController3 : UIViewController 

@property (weak, nonatomic) IBOutlet UIImageView *image1; 
- (IBAction)segmentControl:(id)sender; 
@property (weak, nonatomic) IBOutlet UISegmentedControl *mysegment; 
@property (weak, nonatomic) IBOutlet UIView *colorToBeDisplayedInView; 



@end 

내 ViewController3.m입니다 다음과 같이

내 ViewController3.h 파일입니다

다음과 같이 작성하십시오. 접근 .... 이것은 당신에게 완벽한 출력을 줄 것입니다 ....

+0

다른 코드를 시도해보십시오 .... – Marking

+1

- (void) setSelectedSegmentColor : (UISegmentedControl *) mySegmentedControl { for (int i = 0; i <[mySegmentedControl. 파단 횟수] 난 ++) { 경우 ([mySegmentedControl.subviews objectAtIndex : I]에 isSelected]) { [mySegmentedControl.subviews objectAtIndex : I] setTintColor [UIColor greenColor]; } else { [[mySegmentedControl.subviews objectAtIndex : i] setTintColor : [UIColor redColor]]; 당신이 말한대로 } } } 대신 직접 색조 색상을주는 – Marking

+0

, 내가 이런 짓을 : 대한 (INT I = 0; i가 [_ mysegment.subviews 수를] <; 내가 ++) { 경우 ([[_mysegment titleForSegmentAtIndex : 난 ] isEqualToString : @ "Red"]) { [[_mysegment.subviews objectAtIndex : i] setTintColor : [UIColor redColor]]; } else if ([[_mysegment titleForSegmentAtIndex : i] isEqualToString : @ "Blue"]) { [[_mysegment.subviews objectAtIndex : i] setTintColor : [UIColor blueColor]]; } .... – N4SK

관련 문제