如何通过拨动开关应用设置?

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];

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)