views:

247

answers:

1

I am trying to create something that uses the idea of the 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.

+1  A: 

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

falconcreek
Hi, thanks for the answer, but this seems not to work because I cannot detect the button id. This "sender" points to the segmented control, not to the button. Imagine you have a UIToolBar. Each toolbar element is a button with its own id. This segmented control is a thing with several divisions but each division do not appears to be an id... at least not public. Without having the id I cannot retrieve the button property, as location, frame, etc. I can calculate from the segmented size, but that won't be elegant... :-(
Digital Robot
The 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.Why does your controller need to know the view's size and location details? You may hve to re-think your designThe `selectedSegmentIndex` tells you which segment was selected. A typical usage pattern is to dispatch a message inside a `switch` statement in your `segmentAction` method.
falconcreek
When I re-read your question, I think the answer is that you shouldn't use a segmented control. You need 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.
falconcreek
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.
falconcreek
Another thought. The tradeoff is between implementation difficulty and placement fidelity of the new view. If one assumes that the segments are spaced evenly, you can calculate the position of the new subview using an offset calculation the uses the `selectedSegmentedIndex` value. The y-offset will be some fixed margin. The `x-offset = initialOffset + [sender selectedSegmentIndex] * variableOffset`. If the segmented control with 2 segments is 100 px wide, fixedOffset = 25px (width / (#sections) *2). The variableConstant is 50px (width / #sections).
falconcreek