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

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,560
6,059
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?
 

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,560
6,059
segmentControl.selectedSegmentIndex

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
2008-08-13 20:12:19.654 Say It![8038:20b] Registered that you chose (null)!
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
it's printing out null because you are using the wrong format specifier for an integer. Change the %@ to %d .
 

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,560
6,059
it's printing out null because you are using the wrong format specifier for an integer. Change the %@ to %d .

That prints out:
2008-08-14 08:45:04.731 Say It![8846:20b] Registered that you chose 0!

no matter which segment I select it just prints that it was 0. So does %i.

For whatever reason... %f prints out:

2008-08-14 08:45:54.643 Say It![8866:20b] Registered that you chose -1.992744!

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?
 

keehun

macrumors regular
Mar 17, 2008
110
0
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)

9445479f354001b055c4f737a963afb1.png
 

Attachments

  • segmentTest.zip
    17.9 KB · Views: 98

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,560
6,059
:/

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.)
 

keehun

macrumors regular
Mar 17, 2008
110
0
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
 

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,560
6,059
Ok...

I've attached the folder as a .zip.
 

Attachments

  • Say It!.zip
    18.7 KB · Views: 72

keehun

macrumors regular
Mar 17, 2008
110
0
I think something was just messed up between IB and your class... it works now I zipped it up
 

Attachments

  • Say It!.zip
    28.3 KB · Views: 120
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.