【问题标题】:Giving function for more uiSwitch tags为更多 uiSwitch 标签提供功能
【发布时间】:2014-08-04 22:38:36
【问题描述】:

我想设置,如果我的发件人标签 0-6 的所有开关都关闭,应用程序会显示带有警告消息的警报,以打开至少一个开关。我现在有点不知道该怎么做。

if (switi.tag ==0 && switi.isOn == YES )
    { [self.navigationController popViewControllerAnimated:YES];
        [self willMoveToParentViewController:nil];
}else        if ((switi.tag ==1 && switi.isOn == YES)) {
            [self.navigationController popViewControllerAnimated:YES];
            [self willMoveToParentViewController:nil];

        } else if ((switi.tag ==2 && switi.isOn == YES)) {
            [self.navigationController popViewControllerAnimated:YES];
            [self willMoveToParentViewController:nil];

        } else if ((switi.tag ==3 && switi.isOn == YES)) {
            [self.navigationController popViewControllerAnimated:YES];
            [self willMoveToParentViewController:nil];

        } else if ((switi.tag ==4 && switi.isOn == YES)) {
            [self.navigationController popViewControllerAnimated:YES];
            [self willMoveToParentViewController:nil];

        } else if ((switi.tag ==5 && switi.isOn == YES)) {
            [self.navigationController popViewControllerAnimated:YES];
            [self willMoveToParentViewController:nil];

        } else if ((switi.tag ==6 && switi.isOn == YES)) {
            [self.navigationController popViewControllerAnimated:YES];
            [self willMoveToParentViewController:nil];

        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please put at least one switch on." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];

        }

}

编辑

- (void)viewDidLoad
{
    [super viewDidLoad];



 CGFloat y = 110;
                int counter = 0;
                for (NSString *key in self.kejsss) {
                    UILabel *labeli = [[UILabel alloc] initWithFrame:CGRectMake(50, y, 150, 30)];
                    UISwitch *switi = [[UISwitch alloc] initWithFrame:CGRectMake(210, y, 70, 30)];

                    labeli.tag = counter;
                    switi.tag = counter;

                    labeli.text = key;
                    labeli.textColor = [UIColor whiteColor];
                    switi.on = [[self.konfiggg objectForKey:key] boolValue];
                    [switi addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventTouchUpInside];

                    [self.view addSubview:labeli];
                    [self.view addSubview:switi];

                    self.viewtabulka.layer.borderColor = [UIColor whiteColor].CGColor;
                    self.viewtabulka.layer.borderWidth = 0.5;
                    CALayer *imageLayerRR3 = self.viewtabulka.layer;
                    [imageLayerRR3 setCornerRadius:5];
                    [imageLayerRR3 setBorderWidth:1];
                    [imageLayerRR3 setMasksToBounds:YES];
                    y += 40;
                    counter++;

        }
        }
    }
}
- (IBAction)switchChanged:(UISwitch *)sender{
    kokos= YES;
    [self.konfiggg setObject:@([sender isOn]) forKey:[self.kejsss objectAtIndex:sender.tag]];
    NSLog(@"Selected Switch - %ld (%@)", (long)sender.tag, self.kejsss[sender.tag]);
    NSLog(@"Selected object - %d", [sender isOn]);
}

【问题讨论】:

  • 对于初学者 DRY(不要重复自己):为重复代码创建一个函数。在这种情况下,有更好的算法,请参阅答案。

标签: ios tags uiswitch


【解决方案1】:

这是一种方法。为方便起见,将所有开关组合成一个阵列。然后,每当其中一个开关更改值时,调用一个方法循环遍历数组中的所有开关并检查以确保至少有一个开关仍然打开。你可以这样做:

-(void)checkSwitches {
    // self.switches is an NSArray that holds pointers to all of your UISwitches
    for (UISwitch *s in self.switches) {
        if (s.on == YES) {
            return;
        }
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please put at least one switch on." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

【讨论】:

  • 如果使用 Interface Builder,您可以将它们链接到 IBOutletCollection 而不是 NSArray。
  • 你能检查我的编辑吗?我仍然不知道该怎么做。
  • 在 viewDidLoad 方法中的 for 循环之前创建一个 NSMutableArray (self.switches)。在 for 循环中,将每个开关添加到可变数组 ([self.switches addObject:switi])。
猜你喜欢
  • 1970-01-01
  • 2018-01-29
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多