如何通过拨动开关进行设置?
可能重复:
谁能告诉我如何使用开关?
嗨,我有两个看法
第一个视图包含主要function,第二个视图用于让用户更改第一个视图上的设置。
我在第二个视图中插入一个开关让用户改变第一个视图的设置
我有两个问题,如何使用开关设置YES和NO的值? 然后,根据这个值,第一个视图会有不同的反应?
PS我改变了导航方法的观点,而不是添加子视图
提前致谢
可以说我有以下代码
第一视图
- (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}
第二个view.h
@interface //declaration of the switch - (IBAction) changeMode:(id)sender @end
第二视图
-(IBAction) changeMode:(id)sender { //What to put here to switch ON and OFF ?? }
你可以这样做:
if ( self.mySwitch.on ) // do something
这里我假设mySwitch是UISwitch的一个实例。 将切换值存储到NSUserDefaults是一个好主意,以便您可以从另一个视图检索它。
这是你如何存储的价值:
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];