Search code examples
objective-carraysmethodsibaction

Array of IBAction


- (IBAction)buyHouse[6]:(id)sender;

I want to make an array of IBActions by doing what is shown above. It didn't allow me. Is it possible to make an array of method so as that if I want to implement it, I just need to do this.

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

This IBAction is for many UIButton.

This is my first ever question, so I am truly sorry if it's vague. I'm quite new to Objective-C and this community.


Solution

  • Well as Objective-C is so dynamic, you can call a method just by knowing its name:

    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.