【问题标题】:Trouble Passing Parameter from UiAlertAction从 UiAlertAction 传递参数时遇到问题
【发布时间】:2015-12-01 03:56:20
【问题描述】:

我有一个名为 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 (创造性地命名为 n)方面做错了一些事情,以至于它总是被记住为 0 而不是递增,但我不确定。我尝试了各种类型的数字(NSInteger、int)、强制转换和其他技巧。非常感谢任何帮助!

【问题讨论】:

  • n 在块运行之前不会递增。您需要将其移出 Block 并进入循环体。
  • True J​​osh,但我认为 rmaddy 发现了更大的问题,这是我荒谬的未能注意到我已经从迭代中获得了我的层,因此我不需要使用任何整数来再次获取它哈哈哈

标签: ios objective-c memory increment


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    相关资源
    最近更新 更多