如何以编程方式设置条形按钮项目的操作-c

时间:2012-01-12 04:47:11

标签: objective-c ios uibarbuttonitem

使用下面的代码

实现了一些barbuttons
UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:
    [NSArray arrayWithObjects:@"", nil]] autorelease];
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;

UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

我使用此代码

为按钮设置了set动作
[button addTarget:self 
           action:@selector(drawOnImageByColor:) 
 forControlEvents:UIControlEventTouchUpInside];

3 个答案:

答案 0 :(得分:4)

像这样改变

[button addTarget:self 
           action:@selector(drawOnImageByColor:) 
 forControlEvents:UIControlEventValueChanged];

答案 1 :(得分:2)

使用此代码

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(drawOnImageByColor:)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];

答案 2 :(得分:1)

您必须将要添加的按钮的操作设置为自定义视图。