Segmented Controls...

Discussion in 'iOS Programming' started by ArtOfWarfare, Aug 13, 2008.

  1. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #1
    When a segmented control is selected, how do I determine which segment was selected?

    It looks like I can use

    Code:
    [segmentedControl addTarget:self
                         action:@selector(action:)
               forControlEvents:UIControlEventValueChanged];
    to register another object to watch my segmented control but I don't see how I can get it to recognize which segment got selected.

    Edit: Wait... no...

    I made an IBAction for my controller... and I connected the Segment Control's "Value Changed" event to my action. Now how do I figure out what the value changed to?
     
  2. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
  3. ArtOfWarfare thread starter macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    That seems to just keep returning (null)...

    this is the code I had:

    Code:
    // Controller.h
    
    #import <UIKit/UIKit.h>
    
    
    @interface Controller : NSObject
    {
    	IBOutlet	UISegmentedControl	*numberOfTeams;
    }
    
    - (IBAction) setNumberOfTeams: (id) sender;
    
    @end
    
    // Controller.m
    
    @implementation Controller
    - (IBAction) setNumberOfTeams: (id) sender;
    {
    		NSLog(@"Registered that you chose %@!", numberOfTeams.selectedSegmentIndex);
    }
    @end
    No matter which segment I select it logs
     
  4. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #4
    it's printing out null because you are using the wrong format specifier for an integer. Change the %@ to %d .
     
  5. ArtOfWarfare thread starter macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #5
    That prints out:
    no matter which segment I select it just prints that it was 0. So does %i.

    For whatever reason... %f prints out:

    I'm going to guess that's just because %d and %i are both unsigned while %f is signed... I'll guess -1.992744 is as low as it goes or something like that.

    So... thanks for trying to help... but it still doesn't seem to know the difference between when I try to select different segments. Anyone else think they know what's wrong with it?

    ... do I need to set up my controller to be observing the segment control or something like that for this to work?
     
  6. keehun macrumors regular

    Joined:
    Mar 17, 2008
    #6
    Um, maybe you connected some stuff wrong... I attached a sample project that works as it should.. I deleted the "build" directory because it was so huge.. just open the project and test it out. check the connections and perhaps see what's wrong.

    Do note that I used @property and @synthesize although it honestly doesn't make any difference (I just checked)

    [​IMG]
     

    Attached Files:

  7. ArtOfWarfare thread starter macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
    :/

    Mine still doesn't work.

    ...

    although something I noticed about yours... you connect the action/outlet to "File Owner" while I connect mine to a custom controller object (I posted the code I used for that above.)
     
  8. keehun macrumors regular

    Joined:
    Mar 17, 2008
    #8
    Post your project without the build directory (just so you can post under size limits) and I'll see if I can fix it up and tell you what you did wrong
     
  9. ArtOfWarfare thread starter macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #9
    Ok...

    I've attached the folder as a .zip.
     

    Attached Files:

  10. keehun macrumors regular

    Joined:
    Mar 17, 2008
    #10
    I think something was just messed up between IB and your class... it works now I zipped it up
     

    Attached Files:

  11. ArtOfWarfare thread starter macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #11
    :/

    Well, thanks for making it work.
     

Share This Page