0

我有 2 个 uisegmentedcontrols 的视图,一个在底部,一个在导航栏中。底部的工作正常,导航栏中的那个出现,但没有拾取任何触摸事件。底部段(工作)的代码是:

    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];

导航栏的代码如下(可见但不起作用):

    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);
        }

}
4

1 回答 1

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

这不是您向导航栏添加内容的方式。您必须通过导航控制器的navigationItem. 您可以将分段控件设为其titleView,也可以将其包装在条形按钮项中并将其设为右侧或左侧按钮之一。

于 2013-01-09T19:08:19.947 回答