Ios 无法从UiAlertAction传递参数
我有一个名为MapLayer的自定义NSObject和一个MapLayers的NSMuttableArray,创造性地命名为LayerMuttableArray。按下按钮后,我安装了一个控制器。我使用我的MapLayer列表填充此警报,如下所示:Ios 无法从UiAlertAction传递参数,ios,objective-c,memory,increment,Ios,Objective C,Memory,Increment,我有一个名为MapLayer的自定义NSObject和一个MapLayers的NSMuttableArray,创造性地命名为LayerMuttableArray。按下按钮后,我安装了一个控制器。我使用我的MapLayer列表填充此警报,如下所示: __block NSInteger *n; n = 0; for (MapLayer *m in layersMutableArray) { UIAlertAction *newAction = [UIAlert
__block NSInteger *n;
n = 0;
for (MapLayer *m in layersMutableArray) {
UIAlertAction *newAction = [UIAlertAction actionWithTitle:m.sLayerName style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
MapLayer *ml = layersMutableArray[(int)n];
curLayer = ml;
[self loadSpecificLayer];
n++;
}];
[layerSelectionAlertView addAction:newAction];
}
现在,所有这些都很好。我的AlertView中显示了所有正确的内容
问题是:当我单击一个“层”(UIAlertAction)并调用loadSpecficLayer方法时,它总是重新加载我的第一层。我认为我在内存分配和int(创造性地命名为n)方面做了一些不正确的事情,所以它总是被记为0而不是递增,但我不确定。我尝试过各种类型的数字(NSInteger、int)、施法和其他技巧。非常感谢任何帮助 不要使用
n
。不需要。简单地做:
for (MapLayer *m in layersMutableArray) {
UIAlertAction *newAction = [UIAlertAction actionWithTitle:m.sLayerName style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
curLayer = m;
[self loadSpecificLayer];
}];
[layerSelectionAlertView addAction:newAction];
}
或者,将
n
的增量移动到块外部。n
在块运行之前不会递增。你需要把它移到块外,进入循环体。没错,Josh,但我认为rmaddy发现了更大的问题,那就是我可笑的没有注意到我已经从迭代中得到了我的层,所以我不需要使用任何整数来再次得到它哈哈哈