UISwitch 对值变化执行操作-iphone sdk

发布于 2024-12-07 06:45:35 字数 520 浏览 0 评论 0原文

我有一个 UISwitch,当将开关状态从 OFF 更改为 ON 时,我需要将字符串中的值加载到数组中。我使用了以下代码,但不起作用。

-(IBAction)toggleSwitch1:(id)sender {
    if (addSwitch.on) {
        [addSwitch addTarget:self action:@selector(addName:) forControlEvents:UIControlEventValueChanged];
        NSLog(@"%@",userName);
    }else {
        NSLog(@"No");
    }
}

-(void)addName:(UIControl*)sender {
    [alertList addObject:userName];
    NSLog(@"AlertList: %@", alertList);
}

如果状态更改为 ON 后,如果我重新加载页面,如何保持相同的状态?

请分享您的想法。 谢谢

I have a UISwitch and I need to load the value in a string into an array when change the switch state change to OFF to ON. I have used the following code, but not working.

-(IBAction)toggleSwitch1:(id)sender {
    if (addSwitch.on) {
        [addSwitch addTarget:self action:@selector(addName:) forControlEvents:UIControlEventValueChanged];
        NSLog(@"%@",userName);
    }else {
        NSLog(@"No");
    }
}

-(void)addName:(UIControl*)sender {
    [alertList addObject:userName];
    NSLog(@"AlertList: %@", alertList);
}

And if once the state changed to ON, how can I remain the same state, if I reload the page?

Please share your thoughts.
Thanks

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

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

发布评论

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

评论(2

不疑不惑不回忆 2024-12-14 06:45:36

看起来您遇到的问题是当状态更改时调用toggleSwitch 方法,然后它开始使用addTarget: 方法监听更改。您应该选择其中之一来处理更改。

-(IBAction)toggleSwitch1:(id)sender{
    if (addSwitch.on) {
        [self addName:userName];
    } else {
        //do something else
    }
}

-(void)addName:(UIControl*)sender{
    [alertList addObject:userName];
}

要保存开关状态,只需使用另一个变量来存储开关状态并调用 viewDidLoad 中的 setOn:animated: 方法。

Looks like the issue you're having is that the toggleSwitch method is called when the state changes and then it starts listening for the change using the addTarget: method. You should choose one or the other to handle the change.

-(IBAction)toggleSwitch1:(id)sender{
    if (addSwitch.on) {
        [self addName:userName];
    } else {
        //do something else
    }
}

-(void)addName:(UIControl*)sender{
    [alertList addObject:userName];
}

To save the switch state just have another variable that stores the switch state and call the setOn:animated: method in viewDidLoad.

戏剧牡丹亭 2024-12-14 06:45:35

viewDidLoad 中使用这个:

-(void)viewDidLoad 
{
    [super viewDidLoad];    
    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
    if([userdefault boolForKey:@"yes"]) {
        addSwitch.on=YES;
    } else {
        addSwitch.on=NO;
    }
}

-(IBAction)toggleSwitch1:(id)sender 
{
    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
    if (addSwitch.on) {
        [self performSelector:@selector(addName:) withObject:userName];
        NSLog(@"%@",userName);
        [userdefault setBool:YES forKey:@"yes"];
    } else {
        [userdefault setBool:NO forKey:@"no"];
        NSLog(@"No");
    }
}

-(void)addName:(UIControl*)sender 
{
    [alertList addObject:sender];
    NSLog(@"AlertList: %@", alertList);
}

In viewDidLoad use this one:

-(void)viewDidLoad 
{
    [super viewDidLoad];    
    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
    if([userdefault boolForKey:@"yes"]) {
        addSwitch.on=YES;
    } else {
        addSwitch.on=NO;
    }
}

-(IBAction)toggleSwitch1:(id)sender 
{
    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
    if (addSwitch.on) {
        [self performSelector:@selector(addName:) withObject:userName];
        NSLog(@"%@",userName);
        [userdefault setBool:YES forKey:@"yes"];
    } else {
        [userdefault setBool:NO forKey:@"no"];
        NSLog(@"No");
    }
}

-(void)addName:(UIControl*)sender 
{
    [alertList addObject:sender];
    NSLog(@"AlertList: %@", alertList);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文