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 9318077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:01:34+00:00 2026-06-19T03:01:34+00:00

I have this UILabel *selectedLabel; selectedLabel = nil; if (is_x) { selectedLabel = labelField_x;

  • 0

I have this

UILabel *selectedLabel;
    selectedLabel = nil;
    if (is_x) {
        selectedLabel = labelField_x;
    } else if (is_y) {
        selectedLabel = labelField_y;
    } else if (is_z) {
        selectedLabel = labelField_z;
    }

To prevent this from repeating in the code, how can I make a method that will return a type UILabel class.

I have tried this (it does’t work):

in header (.h) file:

//new method
- (UILabel *) selected;

in implementation (.m) file:

- (UILabel *) selected {
 UILabel *selectedLabel;
    selectedLabel = nil;
    if (is_x) {
        selectedLabel = labelField_x;
    } else if (is_y) {
        selectedLabel = labelField_y;
    } else if (is_z) {
        selectedLabel = labelField_z;
    }
return selectedLabel;
}

- (IBAction)buttonPressed:(id)sender{
 [self selected];

}

How can I return selectedLabel inside the IBAction.

Thank you.

  • 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-06-19T03:01:35+00:00Added an answer on June 19, 2026 at 3:01 am

    Your code looks right, all you need to do is create a UILabel variable in your buttonPressed: method to store the reference to the returned label. So change

    [self selected];
    

    to:

    UILabel *selectedLabel = [self selected];
    // Now do what you want with the selectedLabel
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my appDelegate.m I have this code, but the UILabel is not displaying, but
I have added a UILabel to my view programmatically like this: myLabel = [[UILabel
I have this relatively simple question, I think. Imagine I have a UILabel with
I have taken this code from Changing the background color of a UIAlertView? UIAlertView
I have this code in my viewForHeaderInSection: of tableViewController UILabel *l = [[[UILabel alloc]
I have an UIImageView that has a UILabel on top of it. This UILabel
I have a UILabel with linecount of 0. I set the text for this
I have this in my .h controller @interface helloViewController : UIViewController { IBOutlet UIlabel
So I have something like this in my code: leftMostLabel = [[UILabel alloc] initWithFrame:CGRectMake(60.0f,
I have this code: UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 25, 275, 40)]; label.text

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.