Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-29-2010, 07:56 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 23
appqwer is on a distinguished road
Default How to pass the value to the function when clicking on the UIButton

Hi all,

NSString *name = @"John";

I need to pass the above variable value, when clicking on the uibutton

currently i created the uibutton like below

sendButton = [[UIButton alloc] initWithFrame:CGRectMake(170, 350, 90, 30)];

[sendButton setTitle:@"YES!" forState:UIControlStateNormal];

[sendButton addTarget:self action:@selector(sendAlert forControlEvents:UIControlEventTouchUpInside];

[sendButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

sendButton.backgroundColor = [UIColor whiteColor];

[popoverView addSubview:sendButton];

[sendButton release];

when i clicking the above button, it calls the method but i want to pass the variable (name) to the function...

Please guide me to get it ....
appqwer is offline   Reply With Quote
Old 10-29-2010, 08:21 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 24
craiggil is on a distinguished road
Arrow

In short; I don't think you can. When adding a selector to a UIButton, you can only name a function, that should accept the button as a parameter e.g.

Code:
-(void) buttonWasPressed:(UIButton *)button{...}
You can assign a 'tag' to the button, this is an integer value, then your selector could perform operations based on this tag. There's also the 'performSelector' function you can call, which may be of some use. So, say, the selector from the button takes the *.tag number, then does a 'performSelector' to call another function, this time passing the string you wanted to send. e.g.

Code:
... {
 [buttonOne addTarget:self action:@selector(buttonWasPressed:) forControlEvents:UIControlEventTouchUpInside];
 [buttonTwo addTarget:self action:@selector(buttonWasPressed:) forControlEvents:UIControlEventTouchUpInside];
  buttonOne.tag = 1;
  buttonTwo.tag = 2;
}
...
-(void) buttonWasPressed:(UIButton *)button{
  NSString *name1 = @"John";
  NSString *name2 = @"Jim";
  if (button.tag == 1)  {
    [self performSelector:@selector(someOtherFunction:) withObject:name1]
   } else if (button.tag == 2) {
    [self performSelector:@selector(someOtherFunction:) withObject:name2] 
  }
}

-(void) someOtherFunction:(NSString *)name
{
  NSLog(@"You pushed %@'s button!", name);
}
craiggil is offline   Reply With Quote
Old 10-29-2010, 08:34 AM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by craiggil View Post
In short; I don't think you can. When adding a selector to a UIButton, you can only name a function, that should accept the button as a parameter e.g.

Code:
-(void) buttonWasPressed:(UIButton *)button{...}
You can assign a 'tag' to the button, this is an integer value, then your selector could perform operations based on this tag. There's also the 'performSelector' function you can call, which may be of some use. So, say, the selector from the button takes the *.tag number, then does a 'performSelector' to call another function, this time passing the string you wanted to send. e.g.

Code:
... {
 [buttonOne addTarget:self action:@selector(buttonWasPressed:) forControlEvents:UIControlEventTouchUpInside];
 [buttonTwo addTarget:self action:@selector(buttonWasPressed:) forControlEvents:UIControlEventTouchUpInside];
  buttonOne.tag = 1;
  buttonTwo.tag = 2;
}
...
-(void) buttonWasPressed:(UIButton *)button{
  NSString *name1 = @"John";
  NSString *name2 = @"Jim";
  if (button.tag == 1)  {
    [self performSelector:@selector(someOtherFunction:) withObject:name1]
   } else if (button.tag == 2) {
    [self performSelector:@selector(someOtherFunction:) withObject:name2] 
  }
}

-(void) someOtherFunction:(NSString *)name
{
  NSLog(@"You pushed %@'s button!", name);
}

Craig is right. You can't pass additional parameters as part of button actions. The tag parameter is your only option.

I have often wished that Apple would add a userInfo parameter to UIControls for just such situations as this.

If you really need to do this, create a custom subclass of UIButton and add one or more extra properties to hold your additional info. Then in IB create the buttons as regular buttons, get them all set up, then go to the "identity inspector" and switch the type to your custom button type.

You'll need to set the value of your new property in code though. There is no way to set the value of custom properties in IB.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-29-2010, 10:33 AM   #4 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 24
craiggil is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
I have often wished that Apple would add a userInfo parameter to UIControls for just such situations as this.
Agreed, seems like it would be easy for Apple to extend the UIControls to have an additional NSObject parameter, or be able to specify a "withObject" parameter to the selector.
craiggil is offline   Reply With Quote
Old 10-29-2010, 10:46 AM   #5 (permalink)
Banned
 
Join Date: Jul 2009
Posts: 576
Not_Appy_At_All is on a distinguished road
Default

Quote:
Originally Posted by craiggil View Post
Agreed, seems like it would be easy for Apple to extend the UIControls to have an additional NSObject parameter, or be able to specify a "withObject" parameter to the selector.

I am probably misinterpreting what the OP is trying to do, but...

Can't he/she simply write some additional code inside their original method "sendAlert" to pass that string?
Not_Appy_At_All is offline   Reply With Quote
Old 10-29-2010, 03:41 PM   #6 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 7
JoshNYC is on a distinguished road
Default

You could also create an array where each unique tag equals an item in that array so that when the tag is sent it will retrieve the correct string with something like: [array objectAtIndex:tag];
JoshNYC is offline   Reply With Quote
Old 01-27-2012, 05:03 AM   #7 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 1
Prince is on a distinguished road
Thumbs up How to pass the value when clicking on the UIButton

Hai,
You can use the following button properties to pass parameters on button onclick. button.tag and button.accessibilityValue . For more details visit How to pass a value to a button click - iPhone | Coding Cluster
Prince is offline   Reply With Quote
Old 01-27-2012, 06:24 AM   #8 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Prince View Post
Hai,
You can use the following button properties to pass parameters on button onclick. button.tag and button.accessibilityValue . For more details visit How to pass a value to a button click - iPhone | Coding Cluster
Don't use the accessibilityValue to for programming purposes. That is there to help handicapped people use your app.

Using that for your own purposes is like parking your big smelly SUV in the middle of a wheelchair ramp. Not cool.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone sdk

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 330
12 members and 318 guests
7twenty7, chiataytuday, condor304, Creativ, Domele, dreamdash3, laureix68, LEARN2MAKE, mistergreen2011, mottdog, palme2elie, Paul Slocum
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,660
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, laureix68
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:23 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0