如何在应用程序中保存开关状态

发布于 2024-11-09 04:37:45 字数 2383 浏览 0 评论 0原文

我正在使用开关控件,每当我从页面导航或重新启动应用程序时,其值都会设置为默认“关闭”。如何克服这个问题,以便如果按下它应该保持打开状态,或者如果按下它应该保持关闭状态,直到我们手动更改为止?

这是我的代码,我为滚动视图分配和设置 ffram

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell new";
     listData =[self.tableContents objectForKey:[self.sortedgroups objectAtIndex:[indexPath section]]];


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];

    }
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];


    if(indexPath.section==0)
    {  
        switch (indexPath.row) 
        {
            case 0:
            {
                switcher = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
                [switcher addTarget:self action:@selector(switchAction:)
                   forControlEvents:UIControlEventValueChanged];
                cell.accessoryView = switcher;
                switcher.tag = indexPath.row;


                //cell.textLabel.text=@"switcher";

            } 
                break;
            case 1: 
            {


                slide = [[[UISlider alloc] initWithFrame:CGRectMake(0,0, 150, 15)] autorelease];
                [slide addTarget:self action:@selector(slideact:)
                forControlEvents:UIControlEventValueChanged];
                cell.accessoryView = slide;
                // slide.tag = indexPath.row;                
            } 
                break;

}
}
}

这是我的切换操作方法:

- (void)switchAction:(UISwitch*)sender
{
    NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]);

    if(sender.on)
    {

[[NSUserDefaults standardUserDefaults]setObject:@"on" forKey:@"switcher"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
    else
    {
[[NSUserDefaults standardUserDefaults]setObject:@"off" forKey:@"switcher"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
}

-(void)viewWillAppear:(BOOL)animated
{
if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"switcher"]isEqualToString:@"on"])
 {

switcher.on=YES;

    }
else
{
switcher.on=NO;
}

}

I was working with the switch control, its value sets to default 'off' whenever I navigate from the page or restart the application. How to over come that, so that if pressed on it should stay on or if pressed off it should stay off, until we change manually?

Here is my code where I'm allocating and setting ffram for scrollview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell new";
     listData =[self.tableContents objectForKey:[self.sortedgroups objectAtIndex:[indexPath section]]];


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];

    }
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];


    if(indexPath.section==0)
    {  
        switch (indexPath.row) 
        {
            case 0:
            {
                switcher = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
                [switcher addTarget:self action:@selector(switchAction:)
                   forControlEvents:UIControlEventValueChanged];
                cell.accessoryView = switcher;
                switcher.tag = indexPath.row;


                //cell.textLabel.text=@"switcher";

            } 
                break;
            case 1: 
            {


                slide = [[[UISlider alloc] initWithFrame:CGRectMake(0,0, 150, 15)] autorelease];
                [slide addTarget:self action:@selector(slideact:)
                forControlEvents:UIControlEventValueChanged];
                cell.accessoryView = slide;
                // slide.tag = indexPath.row;                
            } 
                break;

}
}
}

This is my method for switch action:

- (void)switchAction:(UISwitch*)sender
{
    NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]);

    if(sender.on)
    {

[[NSUserDefaults standardUserDefaults]setObject:@"on" forKey:@"switcher"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
    else
    {
[[NSUserDefaults standardUserDefaults]setObject:@"off" forKey:@"switcher"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
}

-(void)viewWillAppear:(BOOL)animated
{
if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"switcher"]isEqualToString:@"on"])
 {

switcher.on=YES;

    }
else
{
switcher.on=NO;
}

}

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

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

发布评论

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

评论(3

如果没有你 2024-11-16 04:37:45

那么同样的,你需要保存开关的状态。
为此,您甚至可以使用数据库或 NSUserDefaults。
使用 NSUserDefaults 会好得多。

- (void)switchAction:(UISwitch*)sender
{
    DEBUGLOG(NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]));

    if(sender.on)
    {

[[NSUserDefaults standardUserDefaults]setObject:@"on" forKey:@"switch"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
    else
    {
[[NSUserDefaults standardUserDefaults]setObject:@"off" forKey:@"switch"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
}

然后在你的 viewwillAppear 方法中执行以下操作

-(void)viewWillAppear:(BOOL)animated
{
if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"switch"]isEqualToString:@"on"]) {

switch.Ison=YES;

    }
else
{
switch.Ison=NO;
}
}

Well sameer then you need to save the state of switch.
For this you can even use the database or NSUserDefaults.
Using NSUserDefaults would be pretty much better.

- (void)switchAction:(UISwitch*)sender
{
    DEBUGLOG(NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]));

    if(sender.on)
    {

[[NSUserDefaults standardUserDefaults]setObject:@"on" forKey:@"switch"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
    else
    {
[[NSUserDefaults standardUserDefaults]setObject:@"off" forKey:@"switch"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
}

Then in your viewwillAppear method do the following

-(void)viewWillAppear:(BOOL)animated
{
if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"switch"]isEqualToString:@"on"]) {

switch.Ison=YES;

    }
else
{
switch.Ison=NO;
}
}
找回味觉 2024-11-16 04:37:45

您应该有一个值来保存可能取决于用户首选项的开关控制状态。当您导航到此视图时,您应该检查此值。

Boolean status=YES;   // You should read this value from User Preference for example it represent the last status set to the switch control.
[switchView setOn:status animated:NO];

有关读取和写入用户首选项的信息,请参阅以下问题:
iphone-reading-and- saving-preferences

You should have a value to save the switch control status may on user preferences. When you navigate to this view you should check for this value.

Boolean status=YES;   // You should read this value from User Preference for example it represent the last status set to the switch control.
[switchView setOn:status animated:NO];

Refer to the following question for reading and writing to User Preferences:
iphone-reading-and-saving-preferences

秋叶绚丽 2024-11-16 04:37:45

您只需使用该方法来设置开关的打开和关闭,如下所示

- (void)switchAction:(UISwitch*)sender
{
    DEBUGLOG(NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]));

    if(sender.on)
    {

    }
    else
    {

    }
}

如果您想将其设置为默认值,那么您只需在应用程序中将其设置为打开,例如在 viewwillappear 的实现文件中或实现后在文件的顶部作为

switch.on=YES;

You simply use the method to set the switch on and off like this

- (void)switchAction:(UISwitch*)sender
{
    DEBUGLOG(NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]));

    if(sender.on)
    {

    }
    else
    {

    }
}

And if you want to set it on default then you just need to set it on in your application say in the implementation file in viewwillappear or at the top of your file after implementaion as

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