6

How can I hook up my UISegmentedControl's value changed method programmatically. I know it's possible using IB but I was wondering how to do it with code. Thanks.

Cœur
  • 37,241
  • 25
  • 195
  • 267
sumderungHAY
  • 1,337
  • 18
  • 30

2 Answers2

17

Attach a target-action for the control event UIControlEventValueChanged.

Example

[segmentedControl addTarget:self action:@selector(valueChanged:) forControlEvents: UIControlEventValueChanged];
Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
4

You can use the addTarget:action:forControlEvents method.

UISegmentControl *mySegmentedControl = [UISegmentControl ...];
[mySegmentedControl addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
Mugunth
  • 14,461
  • 15
  • 66
  • 94