Harita
Harita

Reputation:

Uiswitch on/off

m new to objective-c, i have made a application of login page in which i have used UISwitch to remember d login details if switch is in on mode. i have done with to remember the login details but problem is that how to use the switch on/off condition. Thanx in advance

Upvotes: 7

Views: 27918

Answers (8)

Marcin
Marcin

Reputation: 80

if(switchValue.isOn){
    [switchValue setOn:NO];
} else {
   [switchValue setOn:YES];
}

Upvotes: 0

蘇健豪
蘇健豪

Reputation: 561

This is another solution, if your UISwitch is in a tableView

1 add this code in "tableView:cellForRowAtIndexPath:" method

[switchControl addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];

2 add this method

- (void) switchChanged:(id)sender {
    UISwitch *switchControl = sender;
    NSLog( @"The switch is %@", switchControl.on ? @"ON" : @"OFF" );
}

Upvotes: 0

Jeffrey Neo
Jeffrey Neo

Reputation: 3809

if you want to remember the login details just the moment where the user TURN ON the switch, you can done it by create an Action.

- (IBAction)yourSwitch:(id)sender {
    if([sender isOn]){
        //do your stuff here
    }else{
    }
}

Upvotes: 3

Nat Aguilar
Nat Aguilar

Reputation: 15

This is another solution for that question.

if (switchValue.on == YES)
{
    // Code...
}
else
{
    // Other code... 
}

Upvotes: -1

Taimur Ajmal
Taimur Ajmal

Reputation: 2855

The easiest solution of all :)

if (switchValue.on){
    //Remember Login Details
}
else{
    //Code something else
}

Upvotes: 18

Kaz
Kaz

Reputation: 1466

i had the same problem, i had the name of the UISwitch = Selected

i changed it to another name and it worked.

Upvotes: -1

Jay
Jay

Reputation: 11

I believe the code needs to be this:

if([yourSwitch isOn] == YES) {
    [self rememberLoginDetails];
} else {
    // Do nothing - switch is not on.
}

Upvotes: 0

Tim
Tim

Reputation: 60110

You would add a conditional statement somewhere in your code depending on the switch's on property. Let's say, for example, that you remember login details in a method called rememberLoginDetails. What you would do is, when some action is triggered (the user leaves the login page, for example):

if([yourSwitch isOn]) {
    [self rememberLoginDetails];
} else {
    // Do nothing - switch is not on.
}

The important method here is the isOn method for the UISwitch yourSwitch. isOn is the getter for the switch's on property, which is a BOOL property containing YES if the switch is on, and NO if it is not.

For more information, you can see the UISwitch class reference, specifically the part about isOn.

Upvotes: 7

Related Questions