【问题标题】:Array of IBActionIBAction 数组
【发布时间】:2016-04-01 11:39:46
【问题描述】:
- (IBAction)buyHouse[6]:(id)sender;

我想通过执行上面显示的操作来创建一个 IBActions 数组。它不允许我。是否可以制作一个方法数组,以便如果我想实现它,我只需要这样做。

 - (IBAction)buyHouse[3]:(id)sender{
   _Price.text = [NSString formatWithString: @"hello"];
}

这个IBAction 是许多UIButton 的。

这是我的第一个问题,如果含糊不清,我真的很抱歉。我对 Objective-C 和这个社区很陌生。

【问题讨论】:

    标签: objective-c arrays methods ibaction


    【解决方案1】:

    Objective-C 是如此动态,你只需知道它的名称就可以调用一个方法:

    MyClass.m:

    static NSArray *_methods = nil;
    
    @implementation MyClass
    
    -(id)init
    {
        self = [super init];
        if (self) {
            if (!_methods) {
                _methods = @[
                    @"method1:",
                    @"method2:",
                    @"method3:"
                ];
            }
        }
        return self;
    }
    
    - (void)callMethod:(NSUInteger)index forSender:(id)sender
    {
        SEL selector = NSSelectorFromString(_methods[index]);
        [self performSelector:selector withObject:sender];
    }
    
    - (IBAction)method1:(id)sender
    {
    
    }
    
    // etc.
    

    【讨论】:

    • 运行后,Static NSArray似乎有问题,原因是“[@”method1....];”前面的“@”。它说,“Initializer element is not compile-时间常数。”我试着把它放在任何地方,在实现中,在方法内部,甚至在外部(就像你一样)。
    • @Learner 我经常对什么是编译时常量和什么不是编译时常量感到困惑。我已经编辑了代码,现在应该修复了。
    【解决方案2】:

    您可以使用块代替方法。

    与 IBAction 签名匹配的块的方便 typedef。本例中未使用。

    typedef void(^IBActionBlock)(id sender);
    

    定义您的一系列操作。数组中的每个条目都是一个块,它以单个对象作为参数。

    NSArray *ibActions = @[
                           ^(id sender) {
                               // 0
                           },
                           ^(id sender) {
                               // 1
                           },
                           ^(id sender) {
                               // 2
                           },
                           ^(id sender) {
                               // 3
                           },
                           ^(id sender) {
                               // 4
                           },
                           ^(id sender) {
                               // 5
                           },
                          ];
    

    调用站点示例。

    ibActions[2](button);
    

    【讨论】:

      【解决方案3】:

      您可以使用标签并利用 performSelector 来做到这一点:

      1. 你会为按钮分配标签[你可以在 IB 中这样做。那就是索引]

      2. 您将拥有一个名为 buyHouseByTag 的 IBAction,然后将所有按钮连接到该方法

      3. 你会有 buyHouse1, buyHouse2 ....


      buyHouseByTag 是您的代理:

      - (IBAction)buyHouseByTag:(id)sender {
          NSInteger index = [sender tag];
          NSString *method = [NSString stringWithFormat:@"buyHouse%d:", index];
          SEL sel = NSSelectorFromString(method)
          [self performSelector:sel withObject:sender];
      }
      
      - (IBAction)buyHouse0:(id)sender {
      }
      - (IBAction)buyHouse1:(id)sender {
      }
      - (IBAction)buyHouse2:(id)sender {
      }
      

      【讨论】:

      • 做什么:“SEL sel = NSSelectorFromString(method) [self performSelector:sel withObject:sender];”意思是?
      • method 是一个 NSString,它具有我们想要调用的方法的名称。我们不能,因为 objC 使用选择器来识别方法名称。所以我们从字符串转换为see(ector)。
      • 使用现在转换的选择器,我们告诉运行时调用此方法,将发送者作为参数传递
      猜你喜欢
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-05
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 2011-03-29
      相关资源
      最近更新 更多