首页 文章

麻烦从UiAlertAction传递参数

提问于
浏览
0

我有一个名为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显示了所有正确的东西 .

这是问题:当我点击“图层”(UIAlertAction),然后我调用我的loadSpecficLayer方法时,它总是只重新加载我的第一层 . 我认为我在内存分配和我的int(创造性地 Headers 为n)中做错了,这样它总是被记住为0而不是递增,但我不确定 . 我尝试了各种类型的数字(NSInteger,int),转换和其他技巧 . 任何帮助非常感谢!

1 回答

  • 2

    摆脱 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 的增量移动到块的外部 .

相关问题