Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7444945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:41:56+00:00 2026-05-29T11:41:56+00:00

My knowledge of Xcode and Obj-C is almost inexistant so please be understanding if

  • 0

My knowledge of Xcode and Obj-C is almost inexistant so please be understanding if I pose a stupid/badly formulated question.

I have added a series of buttons in the interface builder.
Now I want to link those buttons to a function (let’s say change a label’s text)
But each button should be able to change the text differently by sending a parameter of some sort.

-(IBAction)changeText:(NSString *) theString{
    myLabel.text=theString;
}

When I add an action to a button I use something like this.

[but1 addTarget:self action:@selector(changeText : myString[1]) 
    forControlEvents:UIControlEventTouchUpInside];

for the second button:

[but2 addTarget:self action:@selector(changeText : myString[2]) 
    forControlEvents:UIControlEventTouchUpInside];

ans so on..

My question is:
If I have a large number of buttons, can I declare them in a loop and add the apropriate action to them at the same time? How do I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:41:57+00:00Added an answer on May 29, 2026 at 11:41 am

    The format for methods called by buttons is fixed. You can’t pass an arbitrary parameter, like a string, you can only pass either nothing, or the button itself.

    This code won’t compile:

    [but1 addTarget:self action:@selector(changeText : myString[1]) forControlEvents:UIControlEventTouchUpInside];
    

    Because the myString[1] part makes your selector invalid. Selectors are just method names, you can’t put parameters inside them. The button decides what parameters to pass when it calls the method, and it always passes itself. You have two choices, you could give each button its own method, like this:

    [but1 addTarget:self action:@selector(changeText1) forControlEvents:UIControlEventTouchUpInside];
    [but1 addTarget:self action:@selector(changeText2) forControlEvents:UIControlEventTouchUpInside];
    etc...
    

    Note the lack of colon at the end of the changeText methods because we aren’t using the button parameter. You would then define your methods as:

    -(IBAction)changeText1 {
        myLabel.text= myString[1];
    }
    
    -(IBAction)changeText2 {
        myLabel.text = myString[2];
    }
    

    But that’s not very scalable. An alternative approach would be to use some property of the button, such as it’s title or tag to branch the code. I think tag would work well for your purposes:

    [but1 addTarget:self action:@selector(changeText:) forControlEvents:UIControlEventTouchUpInside];
    but1.tag = 1; // this will match the index of the string you want to display
    

    Note there is a colon at the end of the changeText: selector now. That means we want the button to pass itself as a parameter when it calls our method. The changeText method will now look like this:

    -(IBAction)changeText:(UIButton *)theButton {
        NSInteger stringIndex = theButton.tag;
        myLabel.text = myString[stringIndex];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a novice iOS developer with a basic knowledge of Xcode. I have one
I have some knowledge on my question but not exactly. What exactly programatically happening
My knowledge about implementing a parser is a bit rusty. I have no idea
My knowledge of xml and php is somewhat basic but have been able to
I have little knowledge of Flash but for a little Flash game I have
We have developers with knowledge of these languages - Ruby , Python, .Net or
My knowledge of C++ arcana is a bit rough. Let's say I have the
I have very little knowledge in testing and I wish to seek guidance in
It is almost common knowledge that one should always compile with -Wall . What
I have very limited knowledge of C#. My goal is to provide a C++

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.