如何添加图像&系统栏按钮项目到UISegmentedControl?

时间:2011-09-13 09:44:07

标签: iphone uibarbuttonitem uisegmentedcontrol

我有一个UISegmentedControl作为导航栏的右侧栏按钮项。这是通过以下代码实现的......

UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Segment1",@"Segment2",nil]];

[segmentedControl addTarget:self action:@selector(segmentClicked) forControlEvents:UIControlEventValueChanged];  //Where segmentClicked is the method for segment click action

segmentedControl.frame = CGRectMake(0, 0, 90, 35);

UIBarButtonItem *rightBaritem = [ [UIBarButtonItem alloc] initWithCustomView:segmentedControl];

[segmentedControl release];

self.navigationItem.rightBarButtonItem = rightBaritem;

[rightBaritem release]; 

以上代码很好,它将显示一个分段控件,其中包含两个段“ Segment1 ”& “分段2 ”。

但我想显示一张图片而不是 Segment1 &系统栏按钮(说UIBarButtonSystemItemAdd)而不是 Segment2

可以通过代码

将图像插入分段控件中
UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:[UIImage imageNamed:@"<image_name.image_type>"],???????,nil]];

但我不知道如何将UIBarButtonSystemItemAdd包含在???????的位置。

先谢谢..

1 个答案:

答案 0 :(得分:1)

这段代码可以解决你的问题。这解决了我同时遇到的问题。但是不要认为这是准确的。我得到了我需要显示的东西。这样可以正常使用两个按钮

UISegmentedControl *doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
        doneButton.momentary = YES; 
        doneButton.frame = CGRectMake(240, 7.0f, 70.0f, 30.0f);
        doneButton.tintColor = [UIColor blackColor];
        [doneButton addTarget:self action:@selector(Done:) forControlEvents:UIControlEventValueChanged];
        [menustyle addSubview:doneButton];
        [doneButton release];

        doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
        doneButton.momentary = YES; 
        doneButton.frame = CGRectMake(10, 7.0f, 70.0f, 30.0f);
        doneButton.tintColor = [UIColor blackColor];
        [doneButton addTarget:self action:@selector(Krishna:) forControlEvents:UIControlEventValueChanged];
        [menustyle addSubview:doneButton];
        [doneButton release];