2017-02-10 4 views
0

私はシミュレータを実行すると、このコードでは何のインデックスが間違って見つけることができませんcuz異なる要素のすべての色が反転されますか?次のようにUISegmentedControlですべてのセグメントの色合いの順序が逆転しているのはなぜですか?どのようにそれを修正するには?

マイViewController3.hファイルは次のとおりです。ここ

#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です:

#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'の2つのセグメントで同じコードを試してから、どのようなo/pが表示されるのかを確認してください。 –

+0

あなたの色合いの色は直接ですが、これは一部の条件では良くありません。だから、文字列に基づいてセグメントの色を設定する...... – Marking

答えて

0

UISegmentedControlの最後のサブビューが

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... 
} 
左にあります

あなたのコードをこのように書くアプローチ.... これはあなたに完璧な出力を与えるでしょう....

+0

他の賢明なこのコードを試してください.... – Marking

+1

- (void)setSelectedSegmentColor:(UISegmentedControl *)mySegmentedControl { for(int i = 0; i <[mySegmentedControl。サブビューカウント]; i)は++ {IF([[mySegmentedControl.subviews objectAtIndex:I] isSelected]) {[[mySegmentedControl.subviews objectAtIndex:I] setTintColor:[UIColor greenColor]。 } else { [[mySegmentedControl.subviews objectAtIndex:i] setTintColor:[UIColor redColor]];あなたが話したよう } } }の代わりに直接色合いの色を与える – Marking

+0

、私はこれをしなかった:のために(int型私= 0;私は[_ mysegment.subviews数を<;私は++){ 場合([[_mysegment titleForSegmentAtIndex:I ] isEqualToString:@ "Red"]){ [[_mysegment.subviews objectAtIndex:i] setTintColor:[UIColor redColor]]; } else if([[_mysegment titleForSegmentAtIndex:i] isEqualToString:@ "Blue"]){ [[_mysegment.subviews objectAtIndex:i] setTintColor:[UIColor blueColor]]; } .... – N4SK

関連する問題