Oh Danny Boy
Oh Danny Boy

Reputation: 4887

Calling function with 1 parameter @ selector.

// Original //

I want to call this guy

-(void)addFavorite:(NSString *)favoriteToAdd

at, @selector here

action:@selector([addFavorite favoriteToAdd:@"string"])];

But I keep getting syntax error no matter which way I write it.

Can someone point out the appropriate way to call this function? When it had no parameter and was "addFavorite," it worked fine.

// Update //

I apologize for not being more specific. This is an iphone application. I have a view with a button, when the button is pressed, an NSString is grabbed and passed to addFavorite (function above). I get syntax errors when attempting to add a parameter to addFavorite.

I want to call the following addFavorite

-(void)addFavorite:(NSString *)favoriteToAdd

Something like this

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
                              initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                              target:self
                              action:@selector([addFavorite: favoriteToAdd:@"testString"])];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];

error: Expected ':' before '[' token

Upvotes: 0

Views: 1299

Answers (2)

Icoder
Icoder

Reputation: 330

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
                              initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                              target:self
                              action:@selector(addFavoriteClick:)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];

This method will be called once the button got tapped In the button click, pass the string which should be added as a parameter as follows

-(void)addFavoriteClick:(UIButton*)sender
{
   NSString *str=@"stringtobeadded";
   [self addFavorite :str];
}

This will help you

Upvotes: 0

Matt S
Matt S

Reputation: 1757

[obj action:@selector(addFavorite:) withObject:@"string"]

Edit: can't spell today :)

One of the methods for calling selector on button click:

[buttonObj target:self action:@selector(addFavoriteClick)]`

You'll then have to PULL the string in addFavoriteClick from where it's defined and pass it into addFavorite:

Upvotes: 4

Related Questions