开发者

iphone - getting the button id in a segmented control

I am trying to create something that uses the idea of th开发者_StackOverflow社区e UIPopOverController, I mean, have that speech bubble anchor pointing to the button who triggered the method.

The problem is that I am using a UISegmentedControl...

How do I get the id of the button that was tapped on a UISegmentedControl, so I can determine its position and generate the anchor?

thanks for any help.


Look at SegmentViewController.{h,m} in the UICatalog sample project.

The sample code sets up the view using code.

This line

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

is where the magic happens.

- (void)segmentAction:(id)sender
{
    //NSLog(@"segmentAction: selected segment = %d", [sender selectedSegmentIndex]);
}

You can also do this in Interface Builder

Assuming that you have (IBAction)segmentAction:(id)sender declared in your ViewController and that the Files Owner is your ViewController, right-click on the UISegmentedControl and make the connection between the UIControlEventValueChanged event to the Files Owner segmentAction

UISegmentedControl acts like a "single" button. It is a single object placed in a single frame unlike the toolbar items which are an array of objects placed in their own frames. A typical usage pattern is to dispatch a message inside a switch statement in your segmentAction method.

I see two options.

Option A: Implement a custom view that contains an array of UIButton controls. The action for the UITouchUpInside event for the all of the buttons can point to the same method in your controller. Then implement a buttonPressed:(id)sender which you can ask the sender for it's frame and location to use to create your new view on top. --> Complicated

Option B: You can calculate the position of the new subview using an offset calculated from selectedSegmentedIndex, widthForSegmentAtIndex:, and contentOffsetForSegmentAtIndex:. Really not much different than calculating from the bounds of any view. --> Easier

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜