【问题标题】:Adding values to NSArray from dynamically created UISwitch elements从动态创建的 UISwitch 元素向 NSArray 添加值
【发布时间】:2015-01-24 01:49:03
【问题描述】:

我有一个需要动态生成部分界面的应用。通过 API 响应,我得到一个 JSON 表示,其中包含我需要创建的每个项目的 ID 和标签。通过几个步骤,我将 JSON 转换为数组并将值保存到 plist。下一步是从保存的值构建接口。

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

我遇到的问题是弄清楚如何获取选定的值并将它们添加到新数组中以发送到 Web 服务。 Web服务部分,我已经处理了。

这是显示如何在视图中获取 UISwitch 元素的代码。我为尝试填充数组而编写的所有其他代码都失败了。所以我也不想让自己难堪!

//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];

   }

}

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

理想情况下,我最终会得到类似的结果:

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

【问题讨论】:

  • 研究 NSMutableArray 的文档。
  • 我已经读过几次了,也许我错过了一些与我正在尝试做的事情相关的事情。但是,我过去在 NSMuteableArray 上取得了很好的成功,并计划在这里使用它。我似乎无法从开关状态中获取值。当我使用 Interface Builder 时这不是问题,因为您可以将每个项目绑定到唯一的 UISwitch 属性。但是当它们是动态生成的时,我不知道如何设置这些值然后将它们传递给我的数组。

标签: objective-c arrays uiswitch


【解决方案1】:

创建另一个具有您切换数组的容量的可变数组。在该数组中,将每个值设置为 0。在 for 循环中创建开关 (elf.qSwitch) 的位置,将开关的标记设置为递增数字。当一个开关被触摸时,通过使用被触摸的开关的标签更新新数组的值,将开关状态(0 或 1)存储在(新的)可变数组中。不值得展示一个例子,但如果这种方法有帮助,请告诉我?

【讨论】:

  • 面对手掌!递增标签正是我所需要的。在你提到它之前,我什至不记得它存在。我会快速尝试一下,如果成功,请将其标记为已接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-21
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多