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];
                        这篇关于如何通过拨动开关应用设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何通过拨动开关应用设置?
				
        
 
            
        基础教程推荐
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
 - Android Volley - 如何动画图像加载? 2022-01-01
 - SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
 - navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
 - Play 商店的设备兼容性问题 2022-01-01
 - UIImage 在开始时不适合 UIScrollView 2022-01-01
 - 如何比较两个 NSDate:哪个是最近的? 2022-01-01
 - 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
 - iOS - UINavigationController 添加多个正确的项目? 2022-01-01
 - Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				