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

Ryan Burgess

macrumors 6502
Original poster
Jan 26, 2013
320
48
Hi, I'm developing a little game in which I want the user to be able to change the settings. I want to use segmented control to do this, but have no experience using segmented control and don't know where to start.

I want to have a display of options (15s, 30s, 45s, 60s) and the user can choose whichever they want. After choosing an option and pressing play, the option they choose should update the seconds variable in the code below:

Code:
 - (void)setupGame {
    // 1
    seconds = 30;
    count = 0;
    highScore = setHigh;
    
    
    
    
    // 2
    timerLabel.text = [NSString stringWithFormat:@"Time: %i", seconds];
    scoreLabel.text = [NSString stringWithFormat:@"Score:\n%i", count];
    highLabel.text = [NSString stringWithFormat:@"High Score:\n%i", highScore];
    
    // 3
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                             target:self
                                           selector:@selector(subtractTime)
                                           userInfo:nil
                                            repeats:YES];
    
    [backgroundMusic setVolume:0.3];
    [backgroundMusic play];
}

How should I go about doing this? Thanks!
 
Okay - so have a look at the Apple Documentation and read about this control.

The code you wrote will never work because you're not telling the seg control what to do on each button (index).

In most cases, you use a switch statement and use the "selected index" to determine which button was tapped and thus, which code gets executed.
 
You should have an outlet to your UISegmentedControl, get the selected index in your code, and map that to a number of seconds somehow, possibly using a switch/case as Tander suggested.
 
You will want to set the IBAction to the valueChanged of the Segmented Control.
 
Last edited:
You will want to set the outlet to the valueChanged of the Segmented Control.

Outlets are pointers to a view object.

IBActions are methods that get triggered when the user does something.

You mean that the OP will want to attach an IBAction to the valueChanged event for the segmented control.

You can either attach an action to valueChanged and do something when the user changes a value, or attach an IBOutlet to the segmented control, and ask the control for it's selected item at the appropriate time. Which approach you use depends on your UI design and what your app does.
 
My bad, you are correct. I was tying it out quickly and it must have typed not what I was thinking. Good catch on the correction.

Outlets are pointers to a view object.

IBActions are methods that get triggered when the user does something.

You mean that the OP will want to attach an IBAction to the valueChanged event for the segmented control.

You can either attach an action to valueChanged and do something when the user changes a value, or attach an IBOutlet to the segmented control, and ask the control for it's selected item at the appropriate time. Which approach you use depends on your UI design and what your app does.
 
You should have an outlet to your UISegmentedControl, get the selected index in your code, and map that to a number of seconds somehow, possibly using a switch/case as Tander suggested.

I agree with ArtOfWarefare.

Since you have a play button, and don't care about the state of the segmented control until the user clicks the play button, do the following:

Set up and IBOutlet to your segmented control and hook it up in IB. Let's call it theSeg:

Code:
IBOutlet UISegmentedControl theSeg;
In your play IBAction, use the IBOutlet to fetch the selected segmented index:

Code:
(IBAction) play: (id) sender;
{
   int index = [theSeg selectedSegmentIndex];

   //Now use the index in a switch statement to set a seconds value
   //Based on which segment the user selected
   
   //Finally, trigger whatever the play button is supposed to do
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.