2012-01-18 28 views
0

我有一個UISegmentedControl包含3個選項來選擇地圖類型。我將tintcolor設置爲默認的黑色,我想爲選定的段設置綠色。UIsegmentedControl setTintColor奇怪的行爲

這裏是分段控制

NSArray *mapType = [NSArray arrayWithObjects: @"Map", @"Hyb", @"Sat", nil]; 
    segmentedMapType = [[UISegmentedControl alloc] initWithItems:mapType] ; 
    segmentedMapType.segmentedControlStyle = UISegmentedControlStyleBar ; 
    segmentedMapType.tintColor = [UIColor blackColor]; 
    segmentedMapType.frame = CGRectMake(100, 40, 120, 40); 
    // segmentedMapType.momentary = YES; 
    [[[segmentedMapType subviews]objectAtIndex:0] setTintColor:[UIColor colorWithRed:0 green:.6 blue:0 alpha:1]]; 
    [segmentedMapType setSelectedSegmentIndex:0]; 

[segmentedMapType addTarget:self action:@selector(selectMap:) forControlEvents:UIControlEventValueChanged]; 
[mapSettingsView addSubview:segmentedMapType]; 

的聲明這裏是選擇方法

-(void)selectMap:(id)sender 
    { 
     int index = [segmentedMapType selectedSegmentIndex]; 

     UIColor *newSelectedTintColor = [UIColor colorWithRed: 0 green:.6 blue:0 alpha:1.0]; 

     if (index==0) { 
    [[[segmentedMapType subviews] objectAtIndex:2] setTintColor:newSelectedTintColor]; 
    [[[segmentedMapType subviews] objectAtIndex:1] setTintColor:[UIColor blackColor]]; 
    [[[segmentedMapType subviews] objectAtIndex:0] setTintColor:[UIColor blackColor]]; 
} else if (index==1){ 
    [[[segmentedMapType subviews] objectAtIndex:1] setTintColor:newSelectedTintColor]; 
    [[[segmentedMapType subviews] objectAtIndex:0] setTintColor:[UIColor blackColor]]; 
    [[[segmentedMapType subviews] objectAtIndex:2] setTintColor:[UIColor blackColor]]; 
} 
else 

if (index==2) 
{ 
    [[[segmentedMapType subviews] objectAtIndex:0] setTintColor:newSelectedTintColor]; 
    [[[segmentedMapType subviews] objectAtIndex:1] setTintColor:[UIColor blackColor]]; 
    [[[segmentedMapType subviews] objectAtIndex:2] setTintColor:[UIColor blackColor]]; 
} 


     } 

此代碼的工作設備上<良好IOS的5,但是,> = IOS的​​5問題,此是綠色色調不適用於正確的部分或有時適用於任何部分。這段代碼有什麼問題?任何幫助將不勝感激。

回答

2

感謝this問題。

如前所述,選擇的索引不是分配tintcolor的可靠方法。我必須存儲他們的id並用它們來分配tintcolor。 第一家門店標識

for (int i=0; i<3; i++) { 
     //The most important trick to work, have to retain the subviews 
     segment[i] = [[[segmentedMapType subviews] objectAtIndex:i] retain]; 
    } 

,然後用它

-(void)selectMap:(id)sender 
{ 
    int index = [segmentedMapType selectedSegmentIndex]; 
    UIColor *newSelectedTintColor = [UIColor colorWithRed: 0 green:.7 blue:0 alpha:1.0]; 
    for (int i=0; i<3; i++) { 
     [segment[i] setTintColor:[UIColor blackColor]]; 
    } 
    int select = segmentedMapType.selectedSegmentIndex; 
    [segment[select] setTintColor:newSelectedTintColor]; 
} 
0

從我所知道的沒有默認的方式來色調特定的分段索引色調顏色..你只能爲整個分段提供色調作爲一個整體。

試試這個

http://www.framewreck.net/2010/07/custom-tintcolor-for-each-segment-of.html

+0

非常感謝Shubhank。但是這段代碼在iOS-4.x中適用於我,當我在iOS-5設備上運行它時,我遇到了此代碼的問題。對不起,我忘記提到這個問題。在你提到的鏈接中,其他開發人員正在抱怨這一點(適用於iOS <5)。 – chatur 2012-01-18 12:45:40

1

出於某種原因,如果它在viewDidLoad中的完成(或viewdDidAppear之前的任何地方)變更爲單段色調的顏色(甚至整個分割控制,不確定)在您觸摸控制器之前不會被應用。