私はシミュレータを実行すると、このコードでは何のインデックスが間違って見つけることができません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
私はそれがすでに質問にあったかどうかわかりません。ここにシミュレータのスクリーンショットへのリンクがあります:https://i.stack.imgur.com/UZ9qC.png – N4SK
'UISegmentaionControl'の2つのセグメントで同じコードを試してから、どのようなo/pが表示されるのかを確認してください。 –
あなたの色合いの色は直接ですが、これは一部の条件では良くありません。だから、文字列に基づいてセグメントの色を設定する...... – Marking