Segmented Control iOS

Discussion in 'iOS Programming' started by Ryan Burgess, Jul 25, 2013.

  1. Ryan Burgess macrumors 6502

    Joined:
    Jan 26, 2013
    #1
    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!
     
  2. Tander macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #2
    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.
     
  3. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    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.
     
  4. KoolStar, Jul 26, 2013
    Last edited: Jul 28, 2013

    KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #4
    You will want to set the IBAction to the valueChanged of the Segmented Control.
     
  5. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #5
    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.
     
  6. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #6
    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.

     
  7. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #7
    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
    }
    
     

Share This Page