Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jjgraz

macrumors regular
Original poster
Feb 13, 2009
103
0
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.
 

Attachments

  • phoneexample.png
    phoneexample.png
    111.4 KB · Views: 71

jjgraz

macrumors regular
Original poster
Feb 13, 2009
103
0
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:mad:"text"];
yourlabel.text = newText;
[newText release];
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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:
Code:
if (index == 0) {
    ...code here...
} else if (index == 1) {
    ...other code here...
}
etc.
 

jjgraz

macrumors regular
Original poster
Feb 13, 2009
103
0
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:mad:"sometext"];
mylabel.text = newText;
[newText release];

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

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.