How to apply setting by toggle switch?(如何通过拨动开关应用设置?)
问题描述
可能重复:
谁能告诉我如何使用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];
这篇关于如何通过拨动开关应用设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何通过拨动开关应用设置?


基础教程推荐
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01