2013-01-09 58 views
0

我有一個2 uisegmentedcontrols,一個沿着底部和一個在導航欄的視圖。最下面的一個工作正常,導航欄中出現一個,但沒有接觸任何觸摸事件。代碼底部段(工作)是:多個uisegctedcontrol在1視圖不能正常工作

footerSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"1", @"2", nil]]; 
[footerSegment setFrame:CGRectMake(5, 6, 310, 30)]; 
[footerSegment setSegmentedControlStyle:UISegmentedControlStyleBar]; 
footerSegment.selectedSegmentIndex = 0; 
//segmentFiltering.tintColor = [UIColor colorWithWhite:80.0/255.0 alpha:1.0]; 
[footerSegment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; 
[footerSegment setTag:0]; 
[footerSegment setBackgroundColor:[UIColor clearColor]]; 
UIImage *segSelected = [[UIImage imageNamed:@"segment_sel.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0]; 
UIImage* menuRightImage = [[UIImage imageNamed:@"button.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0]; 
[footerSegment setBackgroundImage:menuRightImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 
[footerSegment setBackgroundImage:segSelected 
          forState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; 

UIImage *segmentDividerImg = [[UIImage imageNamed:@"div.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0]; 
// Image between two unselected segments. 
[footerSegment setDividerImage:segmentDividerImg forLeftSegmentState:UIControlStateNormal 
       rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 
// Image between segment selected on the left and unselected on the right. 
[footerSegment setDividerImage:segmentDividerImg forLeftSegmentState:UIControlStateSelected 
       rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 
// Image between segment selected on the right and unselected on the right. 
[footerSegment setDividerImage:segmentDividerImg forLeftSegmentState:UIControlStateNormal 
       rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; 

[footerToolBar addSubview:footerSegment]; 

代碼導航欄一個低於(visable但不工作):

headSegment = [[UISegmentedControl alloc] 
       initWithItems:[NSArray arrayWithObjects:@"A", @"B", nil]]; 
[headSegment setFrame:CGRectMake(50, 7, 200, 29)]; 
[headSegment setSegmentedControlStyle:UISegmentedControlStyleBar]; 
headSegment.selectedSegmentIndex = 0; 
[headSegment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; 

[headSegment setBackgroundColor:[UIColor clearColor]]; 
[headSegment setBackgroundImage:menuRightImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 
[headSegment setBackgroundImage:segSelected forState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; 

[headSegment setDividerImage:segmentDividerImg forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 
[headSegment setDividerImage:segmentDividerImg forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 
[headSegment setDividerImage:segmentDividerImg forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; 
[headSegment setTag:1]; 
[self.navigationController.navigationBar addSubview:headSegment]; 

代碼的操作:

-(void)segmentAction:(UISegmentedControl*)sender 
{ 
    if(sender.tag == 0){ 
    NSLog(@"Selected index: %d", sender.selectedSegmentIndex); 
    } 
    else{ 
     if (sender.selectedSegmentIndex = 1) { 
      NSLog(@"Selected HEAD index: %d", sender.selectedSegmentIndex); 
     } 

} 

回答

1
[self.navigationController.navigationBar addSubview:headSegment]; 

這不是你如何將東西添加到導航欄。您必須通過導航控制器的navigationItem工作。您可以將分段控件設置爲titleView,或者您可以將其包裝在酒吧按鈕項中,然後製作右邊或左邊的按鈕之一。

+0

已更改爲self.navigationController.navigationItem.titleView =頭段; – JeffN

+0

它仍然顯示,但我仍然不能選擇一個段 – JeffN

+0

你怎麼知道?您的segmentAction實施並未針對每種可能性進行測試。在沒有任何條件的情況下立即開始登錄。 – matt