Using Segmented Control to change action of button

Discussion in 'iOS Programming' started by lilmul123, Jul 1, 2009.

  1. lilmul123 macrumors newbie

    Joined:
    Jun 30, 2009
    #1
    Hello. I'm a relatively new iphone programmer, but this seemingly simple task is frustrating me.


    On a fully programmatically-created interface, I have three views: two main views, and a settings view. In the settings view, I have a segmented control, and a "done" button. I would like to make it so that when the user picks either of the two segmented options (each cooresponding to a separate view), pushing the done button then takes him to the chosen view.

    Currently, I have tested and confirmed that when i set the action of the done button to either view manually, I can access each view, but when I set up some if-statements that determine the action of the button depending on the chosen segment, it doesn't seem to work. I put this code right after the code that places the done button, and my if statement was similar to this:

    if(segmentedcontrol.selectedSegmentIndex ==0)...

    I'm not absolutely sure if this is the correct method for determining the selected segment...


    My question is, How exactly can I set up my code to work in this fashion? Can I set up a method with the aforementioned if-statement idea or is there something better?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    You'll want this if statement in the code that responds to the Done button being touched. Have you called the addTarget:action:forControlEvents: method for your button?
     
  3. lilmul123 thread starter macrumors newbie

    Joined:
    Jun 30, 2009
    #3
    Yes, that's exactly how I was calling the two actions that correspond to the separate views manually. I then made two separate if-statements that call each action depending on the selected segment, but I did not put them in a method or anything, and this idea didn't seem to work. I'm afraid I don't really know how to do this using a method...

    Is there a way that I can create a method that is called by the action of the done button, and then determines which view method to call depending on which segment is chosen?
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Yes, create the method and then set up the connection to the button using addTarget:action:forControlEvents:. (Hint: Google this addTarget... method and you'll find some examples).

    And then inside this method, you put your if statement.

    This is really pretty basic view controller methodology. I suggest you step back from the real code and review the basics (views, view controllers, buttons, actions) before you continue.
     
  5. lilmul123 thread starter macrumors newbie

    Joined:
    Jun 30, 2009
    #5
    Thanks for your help!

    But, just to make sure, is the correct way to determine which segment is chosen the same as I stated above?

    That is, "if(segmentedcontrol.selectedSegmentIndex ==0)"?
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Correct.
     

Share This Page