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

nottooshabby

macrumors 6502
Original poster
Jul 12, 2008
416
90
I'm trying to set up a Yes/No toggle using a segmented control or just using a switch. Does anybody have an example of setting one of these up using IB? I have it kind of working, but the control keeps defaulting back to No when I leave the view and return. I have been looking at the UICatalog but it's all programatic views, so it's harder to figure out.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Setting it up programmatically or in IB doesn't matter for this.

UISwitch is a very simple control. It has a single property, on. You set it to YES or NO, and of course the user can change it by tapping the control.

You need to have a separate data model object that holds the current value of the choice. Update it via the action method of the switch whenever it's pressed. You need to set this property based on the value in the data model when the switch is going to appear. This is in the viewDidLoad method of the viewController and perhaps also in the viewWillAppear method of the viewController. This design is called MVC.

My guess is that you're not saving the value and restoring it in viewDidLoad or viewWillAppear.
 

nottooshabby

macrumors 6502
Original poster
Jul 12, 2008
416
90
Setting it up programmatically or in IB doesn't matter for this.

UISwitch is a very simple control. It has a single property, on. You set it to YES or NO, and of course the user can change it by tapping the control.

You need to have a separate data model object that holds the current value of the choice. Update it via the action method of the switch whenever it's pressed. You need to set this property based on the value in the data model when the switch is going to appear. This is in the viewDidLoad method of the viewController and perhaps also in the viewWillAppear method of the viewController. This design is called MVC.

My guess is that you're not saving the value and restoring it in viewDidLoad or viewWillAppear.

Thanks. I hadn't wired it up quite right in IB. I had set up the action, but not the delegate. It's working now.....
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.