objective-c Uiswitch 开/关

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1096694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:52:44  来源:igfitidea点击:

Uiswitch on/off

objective-cxcode

提问by Taimur Ajmal

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

我是objective-c的新手,我做了一个登录页面的应用程序,如果开关处于开启模式,我已经使用UISwitch记住d登录详细信息。我已经记住了登录详细信息,但问题是如何使用打开/关闭条件。提前谢谢

回答by Taimur Ajmal

The easiest solution of all :)

最简单的解决方案:)

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

回答by Tim

You would add a conditional statement somewhere in your code depending on the switch's onproperty. 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):

您可以根据开关的on属性在代码中的某处添加条件语句。例如,假设您在名为 的方法中记住登录详细信息rememberLoginDetails。您要做的是,当触发某些操作时(例如,用户离开登录页面):

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

The important method here is the isOnmethod for the UISwitch yourSwitch. isOnis the getter for the switch's onproperty, which is a BOOLproperty containing YESif the switch is on, and NOif it is not.

这里重要的方法是isOnUISwitch的方法yourSwitchisOn是开关on属性的getter ,该BOOL属性包含YES开关是否打开以及NO是否打开。

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

有关更多信息,您可以查看UISwitch 类参考,特别是关于isOn.

回答by Jeffrey Neo

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{
    }
}

回答by u3295047

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

这是另一种解决方案,如果您的 UISwitch 在 tableView 中

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

1 在“tableView:cellForRowAtIndexPath:”方法中添加此代码

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

2 add this method

2 添加这个方法

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

回答by Jay

I believe the code needs to be this:

我相信代码需要是这样的:

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

回答by Nat Aguilar

This is another solution for that question.

这是该问题的另一种解决方案。

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

回答by Kassem

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

我有同样的问题,我有 UISwitch = Selected 的名称

i changed it to another name and it worked.

我把它改成了另一个名字,它起作用了。