无法从 UiAlertAction 传递参数
Trouble Passing Parameter from UiAlertAction
我有一个名为 MapLayer 的自定义 NSObject,以及一个 MapLayers 的 NSMuttableArray,创造性地命名为 layersMutableArray。在按下按钮时,我设置了一个 UIAlertController。我用我的 MapLayers 列表填充此警报,如下所示:
__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 显示了所有正确的内容。
问题在于:当我单击 "layer"(一个 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
的增量移到块外。
我有一个名为 MapLayer 的自定义 NSObject,以及一个 MapLayers 的 NSMuttableArray,创造性地命名为 layersMutableArray。在按下按钮时,我设置了一个 UIAlertController。我用我的 MapLayers 列表填充此警报,如下所示:
__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 显示了所有正确的内容。
问题在于:当我单击 "layer"(一个 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
的增量移到块外。