从动态创建UISwitch元素添加值的NSArray [英] Adding values to NSArray from dynamically created UISwitch elements

查看:149
本文介绍了从动态创建UISwitch元素添加值的NSArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要的接口的一部分,以动态地生成的应用程序。通过API响应,我得到一个JSON重新presentation,包括ID和标签,每个我需要创建项目。过了几个步骤,我将转换为JSON阵列和值保存到一个plist中。下一个步骤是从保存的值建立的接口。

这一切工作按计划进行。我可以动态生成界面元素,它们实际上出现,他们都应该在屏幕上!

我有是搞清楚如何抓住选定的值,并将其添加到新阵列发送到Web服务的问题。 Web服务的一部分,我已经处理了。

下面是code,显示我如何才能在视图中我UISwitch元素。所有其他code我已经写了尝试,并填充数组已经失败。所以,我不想为难自己比我更需要太多了!

  //此方法会从viewDidLoad中被称为
- (无效){setupUI//从根目录获取路径
NSArray的*路径= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);//从列表中获取的文件目录的路径
的NSString * documentPath = [路径objectAtIndex:0];//通过附加的plist的名称,创建一个完整的文件路径
* NSString的plistPath = [documentPath stringByAppendingPathComponent:@settings.plist];//创建字典从plist中值
*的NSDictionary = infoDictionary [NSDictionary的dictionaryWithContentsOfFile:plistPath];从字典内容// Creaet阵列
NSArray的* evqArray = infoDictionary [@evqDict];
NSArray的* exqArray = infoDictionary [@exqDict];//结合阵列
NSMutableArray里* qualifiersArray = [[NSMutableArray里的alloc] initWithCapacity:60];
[qualifiersArray addObjectsFromArray:evqArray];
[qualifiersArray addObjectsFromArray:exqArray];//设置初始y坐标为-35,因为这很容易。
INT yPosStart = -35;//创建为qulifiersArray每个项目UISwitch和标签
对(在qualifiersArray ID标签){    //为数组中的每一个项目,加35个像素xPosStart
    yPosStart = yPosStart + 35;    //转换成整数为CGFloat的定位
    CGFloat的yPosition = yPosStart;    //创建字符串要用于更新UISwitch标签。
    * NSString的= LabelText的标签[@标签];
        的NSLog(@标签EVQ:%@,LabelText的);    *的UILabel = switchLabel [的UILabel页头] initWithFrame:方法CGRectMake(60 yPosition,240,27);    [switchLabel的setText:LabelText的]。    self.qSwitch = [[UISwitch的alloc] initWithFrame:方法CGRectMake(0,yPosition,0,0)];    [self.qSwitch addTarget:自我行动:@选择(changeSwitch :) forControlEvents:UIControlEventValueChanged];    [MyView的addSubview:self.qSwitch];
    [MyView的addSubview:switchLabel];   }}

所以,上述code输出为数组中的每个项目一个UISwitch,但我不知道如何根据用户做出选择创建一个新的数组。

在理想情况下,我最终会得到这样的:

  LABEL1 = YES,
LABEL2 = YES,
LABEL3 = NO,
label4 = YES,
lable5 = NO


解决方案

创建你开关阵列容量的另一个可变数组。在这阵要在其中创建于开关(elf.qSwitch)for循环的每个值设置为0时,开关变量设置为一个递增的数字。当开关被触摸,存储在关闭状态(0或1)通过更新利用被触摸开关的标记的新数组的值(新的)可变数组中为止。没有值得炫耀的一个例子,但让我知道如果这个方法帮助?

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

解决方案

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?

这篇关于从动态创建UISwitch元素添加值的NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆