0

I have this below delegate method that receives stickerURLString as input:

- (void)selectedSticker:(NSString *)stickerURLString {
    //...
        [self.stickerPreviewButton addTarget:self action:@selector(sendStickerPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    //...
}

And the selector calls this method sendStickerPreviewButtonPressed:

- (void)sendStickerPreviewButtonPressed: (NSString *)stickerURLString {
    [self.delegate InputFunctionView:self sendSticker:stickerURLString];
}

As you can see in order to make this work as expected I have to pass on stickerURLString from selectedSticker method to sendStickerPreviewButtonPressed.

I have tried this:

[self.stickerPreviewButton performSelector:@selector(sendStickerPreviewButtonPressed:) withObject:stickerURLString];

instead of this:

[self.stickerPreviewButton addTarget:self action:@selector(sendStickerPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

But I got "terminating with uncaught exception of type NSException" error.

So anyone knows how to pass on string parameter to another method through button selector?

SanitLee
  • 1,253
  • 1
  • 12
  • 29
  • Possible duplicate of [Attach parameter to button.addTarget action in Swift](http://stackoverflow.com/questions/24814646/attach-parameter-to-button-addtarget-action-in-swift) – Bista Sep 22 '16 at 04:37
  • 2
    declare a global variable and assign the value inside your method. – caldera.sac Sep 22 '16 at 04:47
  • @AnuradhS yes it seems the way for me to go about using global variable even though I try to avoid that as my code is quite complex. Thanks! – SanitLee Sep 22 '16 at 08:07

2 Answers2

1

If you are using performSelector to call the function then you can use this variation

  • (id)performSelector:(SEL)aSelector withObject:(id)object;

Documentation

https://developer.apple.com/reference/objectivec/1418956-nsobject/1418764-performselector


You can also check on this answer where another solution is explained, in my opinion probably more complex but useful

https://stackoverflow.com/a/14161831/1502070

Community
  • 1
  • 1
David Velarde
  • 162
  • 1
  • 10
0

For UIControl object function 1st parameter is id type, passes by control itself , you get uibutton object as a parameter in sendStickerPreviewButtonPressed: function which is not convertible to string , So you got an error, You may pass string By extending UIbutton class by your custom class . then Create button of your own class, anr get you custom property form sender button .

     -(void)sendStickerPreviewButtonPressed:(mycustomButton*)sender{
      //sender.myCustomString
}