如何通过拨动开关应用设置? [英] How to apply setting by toggle switch?

查看:16
本文介绍了如何通过拨动开关应用设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
谁能告诉我如何使用switch?

我有两个观点

第一个视图包含主要功能,第二个视图用于让用户更改第一个视图的设置.

the first view contains the main function and the second view is used to let the user change the setting on the first view.

我在第二个视图中插入了一个开关,让用户更改第一个视图的设置

I have insert a switch in second view to let user change a setting on the first view

我有两个问题,如何使用 switch 为一个值设置 YES 和 NO?然后,基于该值第一种观点会有不同的反应?

I have two questions, how to use switch to set YES and NO for a value? and then, base on that value the first view will respond differently?

附:我用导航方法改变视图,而不是添加子视图

P.S. I change the view with navigation method , not add subview

提前致谢

假设我有以下代码

第一个视图.m

- (IBAction) addValue:(id)sender
{
aValue ++;
}
//the following will react according to the setting on second view
If (????//Don't know what to put here to represent the switch is ON or OFF)
{ //whatever action}
else
{//whatever action}

第二个视图.h

@interface
    //declaration of the switch
        - (IBAction) changeMode:(id)sender
    @end

第二个视图.m

    -(IBAction) changeMode:(id)sender
{
    //What to put here to switch ON and OFF ??
}

推荐答案

你可以这样做:

if ( self.mySwitch.on )
// do something

这里我假设 mySwitch 是 UISwitch 的一个实例.将开关值存储到 NSUserDefaults 是个好主意,这样您就可以从另一个视图中检索它.

Here I assume mySwitch is an instance of UISwitch. It is a good idea to store switch value to NSUserDefaults so that you can retrieve it from another view.

这是您存储值的方式:

NSString *switchValue; 
if ( self.mySwitch.on ) 
    switchValue = @"YES";
else 
    switchValue = @"NO";

[[NSUserDefaults standardUserDefaults] setObject:switchValue forKey:@"switchValue"];
[[NSUserDefaults standardUserDefaults] synchronize];

这是您检索值的方式:

BOOL switchState = [[[NSUserDefaults standardUserDefaults] objectForKey:@"switchValue"] boolValue];

这篇关于如何通过拨动开关应用设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆