如何申请通过拨动开关设置? [英] How to apply setting by toggle switch?
问题描述
可能重复:结果
谁能告诉我怎么用开关?
块引用>您好我有两个看法
第一种观点中包含的主要功能和第二个观点是用来让用户改变在第一个视图中的设置。
我插入第二个视图切换,让用户改变在第一个视图设置
我有两个问题,如何使用开关设置YES和NO的价值?然后,基于该值的
第一种观点将不同的反应?P.S。我改变视图导航方法,而不是添加子视图
在此先感谢
可以说我有以下的code
首先View.m
- (IBAction为)的addValue:(ID)发送者
{
安勤++;
}
//下面将根据对第二种观点的设置作出反应
如果(???? //不知道该怎么把这里重新present开关ON或OFF)
{//任何行动}
其他
{//任何行动}第二view.h
@interface
//开关的声明
- (IBAction为)changeMode:(ID)发送者
@结束第二view.m
- (IBAction为)changeMode:(ID)发送者
{
//放什么在这里开启和关闭?
}
解决方案您可以这样做:
如果(self.mySwitch.on)
// 做一点事在这里,我假设是myswitch是UISwitch的一个实例。它是存储交换机的价值,以便NSUserDefaults的,你可以从另一个观点检索是一个好主意。
这是你如何存储的值:
的NSString * switchValue;
如果(self.mySwitch.on)
switchValue = @YES;
其他
switchValue = @否;[NSUserDefaults的standardUserDefaults]的setObject:switchValue forKey:@switchValue];
[NSUserDefaults的standardUserDefaults]同步]。这是你如何检索值:
BOOL switchState = [[[NSUserDefaults的standardUserDefaults] objectForKey:@switchValue] boolValue];
Possible Duplicate:
can anyone tell me how to use switch ?Hi I have two view
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
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
Thanks in advance
lets say I have the following code
First View.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}
second view.h
@interface //declaration of the switch - (IBAction) changeMode:(id)sender @end
second view.m
-(IBAction) changeMode:(id)sender { //What to put here to switch ON and OFF ?? }
解决方案You can do this:
if ( self.mySwitch.on ) // do something
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.
This is how you store the value:
NSString *switchValue; if ( self.mySwitch.on ) switchValue = @"YES"; else switchValue = @"NO"; [[NSUserDefaults standardUserDefaults] setObject:switchValue forKey:@"switchValue"]; [[NSUserDefaults standardUserDefaults] synchronize];
This is how you retrieve the value:
BOOL switchState = [[[NSUserDefaults standardUserDefaults] objectForKey:@"switchValue"] boolValue];
这篇关于如何申请通过拨动开关设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!