Good example using a UISwitch or segmented control in IB?

Discussion in 'iOS Programming' started by nottooshabby, Sep 21, 2008.

  1. nottooshabby macrumors 6502

    Jul 12, 2008
    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.
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    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.
  3. nottooshabby thread starter macrumors 6502

    Jul 12, 2008
    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.....

Share This Page