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

发布于 2024-11-01 03:53:11 字数 974 浏览 4 评论 0原文

可能的重复:
谁能告诉我如何使用 switch 吗?

嗨,我有两个视图

第一个视图包含主要功能,第二个视图用于让用户更改第一个视图上的设置。

我在第二个视图中插入了一个开关,以让用户更改第一个视图上的设置

我有两个问题,如何使用开关为值设置“是”和“否”?然后根据该值 第一个视图会有不同的反应吗?

PS 我用导航方法更改视图,而不是添加子视图

提前感谢

让我说我有以下代码

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}

secondary view.h

@interface
    //declaration of the switch
        - (IBAction) changeMode:(id)sender
    @end

secondary view.m

    -(IBAction) changeMode:(id)sender
{
    //What to put here to switch ON and OFF ??
}

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 ??
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佼人 2024-11-08 03:53:11

你可以这样做:

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文