setValue UISwitchControl?

Discussion in 'iOS Development' started by jeppeb, Apr 6, 2008.

  1. jeppeb

    jeppeb New Member

    Joined:
    Apr 3, 2008
    Messages:
    14
    Likes Received:
    0
    Hey all,

    How can i set the value (on/off (YES/NO)) of a UISwitchControl?
    This doesn't seem to work:
    Code:
    switchControl = [[UISwitchControl alloc] initWithFrame:CGRectMake(210.0f, 11.0f, 114.0f, 45.0f)];
    [switchControl setValue: YES];
    What am i doing wrong?

    Thanks!
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    As far as I know, a switch is a speciised type of slider that only registers "all or nothing." It works on a scale of 0.0 and 1.0 (both floats).

    Try setting the value to either 0 or 1.


    Although, if you are setting up anything that needs to be remembered in the app's setting, you van just call the value from your settings file and use boolValue. Or, if you're using NSUserDefaults, you can use boolForKey.
  3. stratocaster2006

    stratocaster2006 New Member

    Joined:
    Dec 26, 2007
    Messages:
    22
    Likes Received:
    0
    settings value of switchControl

    this is what I use:

    UISwitchControl *switchControl = [(UIView *)[UISwitchControl alloc] initWithFrame:CGRectMake(origin.x, origin.y, width, height)];

    if (myGlobalBoolean)
    [switchControl setValue: 1.0f ];
    else
    [switchControl setValue: 0.0f ];

    SwitchControls have a float value, since they can slide from minValue to maxValue. That is because they were derived from UISliderControl.

    You can actually set those as well using following:

    [switchControl setMinValue: 2.25f];

    [switchControl setMaxValue: 4.75f];

    But there is little value in this, unless you happen to need to switch between those float values.

    So they are a special case, in that they switch to only two states, but the value of those states can return any float value.

    For extra functions of the UISwitchControl, check out the UISliderControl methods.

    hope this helps.

Share This Page