1

I am trying to pass a NSString parameter through action:@selector. I have tried many things such as

action:@selector(function:example)
action:@selector(function:example)
action:@selector(function) withObject:example

None of these are working for me and I have no Idea what to do.

Here is the method I am trying to select

- (void) function: (NSString *)testString{
//Whatever stuffz
}

And here is the complete line of code the selector is in

[testButton addTarget:self action:@selector(function:) forControlEvents:UIControlEventTouchUpInside];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Max
  • 318
  • 1
  • 3
  • 11
  • Not totally clear what your situation is. Can you post the method signature that you're trying to get the selector of? And what method are you trying to call with it?> – Ben Zotto May 31 '15 at 02:57
  • Why do you pass a NSString? – Bannings May 31 '15 at 03:04
  • I am trying to send a name of a database from a function through the press of a button. I hope that makes sense. – Max May 31 '15 at 03:05

2 Answers2

2

You cannot directly send different parameter using the addTarget:action:forControlEvents: it will just send it self(UIButton *) to the @selector.

Meaning what you are doing is literally like: [self function:< testButton >];

What you really want to do is:

[testButton addTarget:self action:@selector(testButtonAction:) forControlEvents:UIControlEventTouchUpInside];

- (void)testButtonAction:(UIButton *)sender
{
    [self performSelector:@selector(function:) withObject:@"yourString"];
}

- (void) function:(NSString *)testString
{
    NSLog(@"testString :%@", testString);
}

Hope this helps..

0yeoj
  • 4,500
  • 3
  • 23
  • 41
0

You can pass value to selector as below

[self performSelector:@selector(function:) withObject:@"myString"];

You can't pass a string to button action, iOS pass button itself to the method. If you want to use any string value there in method then you need to declare a variable or method which will return the particular string.

kmithi1
  • 1,737
  • 2
  • 15
  • 18