Adding values to NSArray from dynamically created UISwitch elements

心不动则不痛 提交于 2019-12-13 00:49:14

问题


I have an app that needs part of the interface to be generated dynamically. Via an API response, I get a JSON representation, containing an ID and label, for each item I need to create. Over a couple of steps, I convert the JSON to arrays and save the values to a plist. Next step is to build the interface from the saved values.

This all works as planned. I can dynamically generate the interface elements and they actually show up on the screen where they are supposed to!

The problem I am having is figuring out how to grab the selected values and add them to a new array for sending to a web service. The web service part, I have handled.

Here is the code that shows how I get my UISwitch elements in the view. All other code I have written to try and populate an array has been a flop. So I don't want to embarrass myself any more than I need too!

//this method gets called from viewDidLoad
-(void) setupUI {

//Get path from the root directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

// Get a path to the documents directory from the list
NSString *documentPath = [paths objectAtIndex:0];

// Create a full file path by appending the name of plist
NSString *plistPath = [documentPath stringByAppendingPathComponent:@"settings.plist"];

// Create dictionary from values in plist
NSDictionary *infoDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];

// Creaet Arrays from dictionary contents
NSArray *evqArray = infoDictionary[@"evqDict"];
NSArray *exqArray = infoDictionary[@"exqDict"];

// Combine arrays
NSMutableArray *qualifiersArray = [[NSMutableArray alloc] initWithCapacity:60];
[qualifiersArray addObjectsFromArray:evqArray];
[qualifiersArray addObjectsFromArray:exqArray];

//Set initial y coordinate to -35 because it's easy.
int yPosStart = -35;

// Create a UISwitch and label for each item in qulifiersArray
for (id label in qualifiersArray) {

    // for every item in the array, add 35 pixels to the xPosStart
    yPosStart = yPosStart + 35;

    // Convert integer into CGFloat for positioning
    CGFloat yPosition = yPosStart;

    // Create a string to be used for updating the UISwitch label.
    NSString *labelText = label[@"label"];
        NSLog(@"evq labels:  %@", labelText);

    UILabel *switchLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, yPosition, 240, 27)];

    [switchLabel setText:labelText];

    self.qSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, yPosition, 0, 0)];

    [self.qSwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];

    [myView addSubview:self.qSwitch];
    [myView addSubview:switchLabel];

   }

}

So, the above code outputs a UISwitch for every item in the array, but I have no idea how to create a new array based on the selections a user makes.

Ideally, I would end up with something like:

label1 = YES,
label2 = YES,
label3 = NO,
label4 = YES,
lable5 = NO

回答1:


Create another mutable array with the capacity of you switches array. In that array set each value to 0. Where you are creating switches (elf.qSwitch) in the for loop, set the tag of the switch to an incrementing number. When a switch is touched, store the on off state (0 or 1) in the (new) mutable array by updating the value of the new array utilising the tag of the switch that was touched. Not worth showing an example but let me know if this approach helps?



来源:https://stackoverflow.com/questions/27137203/adding-values-to-nsarray-from-dynamically-created-uiswitch-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!