把self.editbuttonitem作为barbuttonitem分段控制?

时间:2011-08-27 02:00:24

标签: iphone ios uibarbuttonitem uisegmentedcontrol

我想创建一个类似于NavBar示例代码示例3中的NavBar的NavBar,除了我想使用self.editbuttonItem作为SegmentedControl中的两个按钮之一。 (另一个是自定义添加按钮。)基本上 - 最终结果将是一个leftBarButtonItem,它只是一个按钮,显示一个模态视图,一个rightBarButtonItem,它是一个带有编辑和添加按钮的分段控件。

事情是,看起来设置SegmentedControl需要一个字符串或图像数组,而不是BarButtonItems。有解决方法吗?

这是Apple样本中的相关内容:

    // "Segmented" control to the right
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                            [NSArray arrayWithObjects:
                                                [UIImage imageNamed:@"up.png"],
                                                [UIImage imageNamed:@"down.png"],
                                             nil]];
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;

defaultTintColor = [segmentedControl.tintColor retain]; // keep track of this for later

UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];

self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];

而不是图像,我想放置BarButtonItems ......

1 个答案:

答案 0 :(得分:0)

如果self.editbuttonitem是标题为“编辑”的UIBarButtonItem,我认为您可以使用以下代码

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

根据Charles Bandes的评论进行修改

segmentedControl添加操作,就像Apple的示例:

[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

//...

然后在segmentAction:中,执行

- (void)segmentAction:(UISegmentedControl*)sender
{
    //if the "edit" item in segmentedControl is selected
    if (sender.selectedSegmentIndex == 0)
    {
        //I assume self is a UITableView instance
        //start editing
        [self setEditing:YES animated:YES];
    }
}

我在电脑上写了这些代码。但它应该有用。

您可以查看[UITableView setEditing:animated:]