PDA

View Full Version : control & label




jjgraz
Mar 7, 2009, 05:37 PM
I have all the outlets complete. Does anyone have a code example or can tell me how to have the UILabel change when user taps a different button on this segmented control. I have uploaded a picture for complete clarity.

SEE ATTACHMENT.



dejo
Mar 7, 2009, 06:18 PM
Do you have a target-action pairing set up for UIControlEventValueChanged?

jjgraz
Mar 7, 2009, 07:04 PM
I don't. Can you explain?

dejo
Mar 7, 2009, 07:08 PM
I don't. Can you explain?
The documentation for the UISegmentedControl should help get you started. Pay special attention to the Overview.

jjgraz
Mar 7, 2009, 08:00 PM
Thanks.....I'll give it a keen eye.

jjgraz
Mar 9, 2009, 02:14 PM
I was able to complete the task by creating an action and creating 5 if statements within the action for each SegmentIndex (0,1,2,3,4). Below is a snip of code.


- (IBAction)changeSeg {
if (segControl.selectedSegmentIndex == 0) {
NSString *newText = [[NSString alloc] initWithFormat:@"text"];
yourlabel.text = newText;
[newText release];

dejo
Mar 9, 2009, 02:25 PM
I was able to complete the task by creating an action and creating 5 if statements within the action for each SegmentIndex (0,1,2,3,4).
Great. You could also have used a switch construct to achieve what the if statements do.

Also, if you decide to stick with the if statements, I'd suggest building them in an "if-else if"-type construct to avoid unnecessary logic checking. I.E. if you know the selectedSegmentIndex == 0, then you don't need to test it against any of the other possibilities. So, like this:
if (index == 0) {
...code here...
} else if (index == 1) {
...other code here...
}
etc.

jjgraz
Mar 9, 2009, 03:24 PM
You mean adding the word else limits the amount of logic checking making my application run more efficiently....? Sounds like a winner. I appreciate your help.

sample:

- (IBAction)changeSeg {
if (segControl.selectedSegmentIndex == 0) {
NSString *newText = [[NSString alloc] initWithFormat:@"sometext"];
mylabel.text = newText;
[newText release];

}
else if (segControl.selectedSegmentIndex == 1) {
NSString *newText = [[NSString alloc] initWithFormat:@"somemoretext"];
mylabel.text = newText;
[newText release];

dejo
Mar 9, 2009, 03:56 PM
You mean adding the word else limits the amount of logic checking making my application run more efficiently....? Sounds like a winner. I appreciate your help.
You're welcome. This is pretty basic Objective-C, heck even plain ol' C, coding
practices. I'd suggest stepping back and learning the basics of the language before continuing on with any real programming.