control & label

Discussion in 'iPhone/iPad Programming' started by jjgraz, Mar 7, 2009.

  1. macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #1
    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.
     

    Attached Files:

  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Do you have a target-action pairing set up for UIControlEventValueChanged?
     
  3. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #3
    I don't. Can you explain?
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    The documentation for the UISegmentedControl should help get you started. Pay special attention to the Overview.
     
  5. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #5
    Thanks.....I'll give it a keen eye.
     
  6. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #6
    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];
     
  7. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    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.
     
  8. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #8
    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];
     
  9. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    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.
     

Share This Page