Wednesday, 11 September 2013

How to react on a click of a UISegmentedControl?

How to react on a click of a UISegmentedControl?

How can I trigger my method that is checking if the UISegmentedControl
index has ben changed.
What I have tried:
I have created the Outlet of my control
@property(nonatomic,strong)IBOutlet UISegmentedControl *sgclanguage;
And here I add my UISegmentedControl in my view as you can see its all
done programmatically.
NSArray *itemArray = [NSArray arrayWithObjects: @"German",
@"English",nil];
[sgclanguage = [UISegmentedControl alloc] initWithItems:itemArray];
sgclanguage.frame = CGRectMake(35, 200, 250, 50);
sgclanguage.segmentedControlStyle = UISegmentedControlStylePlain;
sgclanguage.selectedSegmentIndex = 0;
[sgclanguage setFrame:CGRectMake(viewFrame.origin.x+820,
viewFrame.origin.y+33, 200,35)];
[sgclanguage setBackgroundColor:[UIColor clearColor]];
[sgclanguage setSegmentedControlStyle:UISegmentedControlStyleBar];
[self addSubview:sgclanguage];
}
And here is my action that I wish that would be triggered when I change
the index
-(IBAction)segmentedControlIndexChanged:(id)sender
{
switch (self.sgclanguage.selectedSegmentIndex) {
case 1:
NSLog(@"Hallo");
break;
default:
NSLog(@"Hallo123");
break;
}
}
How can I trigger this action out of this stuff that I have made .. and
btw is the approche good or would you do it different ?
Thanks for help and fast answer

No comments:

Post a Comment