Question

At a certain point in my app, the user is presented with tons of UISwitches. When this view leaves, I want to check the position of each switch, and if it is on, to save a simple string into core data for that switch just letting me know it is on. The problem is, I don't want to manually make a bunch of if statements like the following:

if(switch1 is on){
    save attribute in core data switch1checked as @"YES i am checked"
}

what would be the best practice for checking different UISwitches with different outlet names? I was thinking something like below:

for (UIView *view in [self.scrollView subviews]) {
    if ([view isKindOfClass:[UISwitch class]]) {
        UISwitch *switchView = (UISwitch *)view;
        if(switchView.isOn == YES){
            [self saveToCoreData];
        }

    }
}

The problem isn't saving to Core Data, the problem is how to know which switch it is on. Any ideas? I'm sure it's simple, but I can't think of a solution.

Was it helpful?

Solution

Add a tag to each switch. Don't wait for the view to be closed, add a target and action to each of your switches and handle the value change. When the value changes, use the tag to determine which switch changed and save that data immediately.

The tag could be an index into an array of switch identifiers...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top