0

Usually I'm assigning single selector to multiple UIButtons in this way only,

    [buttonOne addTarget:self action:@selector(someAction:) forControlEvents:UIControlEventTouchUpInside];
    [buttonTwo addTarget:self action:@selector(someAction:) forControlEvents:UIControlEventTouchUpInside];
    [buttonThree addTarget:self action:@selector(someAction:) forControlEvents:UIControlEventTouchUpInside];

Today I found another way,

    SEL selector = NSSelectorFromString(@"someAction:");

    [buttonOne addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    [buttonTwo addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    [buttonThree addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];

Please note that, I'm assigning this selector in table to each rows with above buttons.

Which way is more efficient? I assuming second way because we're creating selector for once, but not sure.

Hemang
  • 26,840
  • 19
  • 119
  • 186

2 Answers2

1

Essentially, they're the same. It's just like

NSString *myTest = @"My text";
label1.text = myText;
label2.text = myText;

compared to

label1.text = @"My text";
label2.text = @"My text";

You aren't creating a selector, you're just referring to it in different ways. No memory leaks or nothing. Hope you get my point.

Akshay Bhalotia
  • 799
  • 4
  • 11
  • @Hemang Consider marking the answer as "accepted" as it helps other users to look at correct places and choose the best answers. – Akshay Bhalotia Nov 04 '14 at 10:48
0

as per the @Libran Coder answer, I've double check my self to prove which one is best.

SEL selector = NSSelectorFromString(@"someAction:");

NSLog(@"%p",selector);

NSLog(@"%p",NSSelectorFromString(@"someAction:"));

both will at the same memory address.

Hemang
  • 26,840
  • 19
  • 119
  • 186