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

lilmul123

macrumors newbie
Original poster
Jun 30, 2009
18
2
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?
 
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?
 
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?
 
Is there a way that I can create a method that is called by the action of the done button...
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 determines which view method to call depending on which segment is chosen?
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.
 
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)"?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.