How to I respond to a user pressing a UISegment? Is there a delegate, or must I programmatically (or Interface Builder), attach the开发者_运维知识库 selectors?
If you prefer to do it in code you can use:
[segmentedControl addTarget:self action:@selector(didSelectIndex:) forControlEvents:UIControlEventValueChanged];
And this is then the method that would be called
- (void) didSelectIndex: (id) sender
{
NSLog(@"%@",[(UISegmentedControl *)sender titleForSegmentAtIndex:[(UISegmentedControl *)sender selectedSegmentIndex]]); //replace this with your code
}
If you prefer to use IB, right click on your UISegmentedControl select Value Changed
and then attach it to the desired method in your first responder.
UISegmentedControl subclasses UIControl and sends out the control event UIControlEventValueChanged
whenever the selected segment changes. You can add a target/action pair for this event in code, or you can do it with the normal control-click-and-drag in IB.
精彩评论