0

正在更改所选段的颜色。它工作正常,但 uisegment 控制色调颜色设置为默认值,直到它第一次被触摸。

这是更改所选片段颜色的方法。它工作正常,但是当该片段第一次出现时。它有浅灰色。然后当被触摸时,它会根据我的需要开始正常工作。

(此段控件作为子视图添加到 uialertview)

 -(void)segmentValueChanged:(UISegmentedControl*)sender 
  {

        for (int i=0; i<[sender.subviews count]; i++)
        {
            if ([[sender.subviews objectAtIndex:i]isSelected] )
            {
                UIColor *tintcolor=[UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
                [[sender.subviews objectAtIndex:i] setTintColor:tintcolor];

                trackType = sender.selectedSegmentIndex;
            }
            else{
                UIColor *tintcolor=[UIColor colorWithRed: 225/255.0 green:220/255.0 blue:210/255.0 alpha:1.0];
                [[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
            }

        }

    }
4

4 回答 4

4

您可以在viewDidLoadsegmentControlValueChanged方法中强制调用方法。像这样:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.05 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [self segmentedControlValueChanged:segmentedControlOne];
    }); 
于 2013-05-31T06:45:39.983 回答
1

验证这段代码

我用这段代码解决了我的应用程序

- (void)viewDidAppear:(BOOL)animated

    {

        for(UIView *v in [self.view subviews] )
        {
            if([v isKindOfClass:[UISegmentedControl class]])
            {
               // ((UISegmentedControl*)v).enabled=NO;

                dispatch_async(dispatch_get_main_queue(),^{

                    for (int i=0; i<[ ((UISegmentedControl*)v).subviews count]; i++)

                    {

                        if ([[((UISegmentedControl*)v).subviews objectAtIndex:i]isSelected] )

                        {

                            [[((UISegmentedControl*)v).subviews objectAtIndex:i] setTintColor:[UIColor colorWithRed: 0/255.0 green:176/255.0 blue:223/255.0 alpha:1.0]];
                            //[[((UISegmentedControl*)v).subviews objectAtIndex:0]setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                            //  break;

                        }
                        else
                        {
                            [[((UISegmentedControl*)v).subviews objectAtIndex:i] setTintColor:[UIColor whiteColor]];
                            //[[((UISegmentedControl*)v).subviews objectAtIndex:0]setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

                        }


                    }


                });

            }
        }

    }
于 2013-05-31T06:46:17.060 回答
1

您能否从您的视图的 viewdidload 或 viewwillappear 方法中更改它?因为根据您的代码,当用户第一次触摸它时,它会改变。

于 2013-05-31T06:38:00.767 回答
0

我自己想通了。由于 alertview 仅在 [alertview show] 之后创建所有视图。对段控制的颜色更改不受影响,因为段控制还没有真正创建。所以我所做的是我延迟了segmentcontrol tintcolor更改的过程,直到它由alertview创建

[alertview show];

double delayInSeconds = 0.01;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [self performSelectorOnMainThread:@selector(segmentValueChanged:) withObject:segControl waitUntilDone:YES];

});
于 2013-05-31T07:39:05.767 回答