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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:58:11+00:00 2026-05-12T11:58:11+00:00

I have three button named(titled) hello, nothing, heaven and one label (IBOutlet UIlabel lab).

  • 0

I have three button named(titled) hello, nothing, heaven and one label (IBOutlet UIlabel lab). I want to display three diff messages for three diff button click. But the following code failed to accomplish this. Can anyone suggest any idea?

-(IBAction)buttonclick:(id)sender  
{  

    NSString *title=[sender titleForState:UIControlStateNormal];

    if([title isEqualToString:@"hello"])
    {

        NSString *str=[[NSString alloc] initWithFormat:@"abc"];
    }
    else if([title isEqualToString:@"nothing"]) {

        NSString *str=[[NSString alloc] initWithFormat:@"def"];
    }
    else if([title isEqualToString:@"heaven"])
    {

        NSString *str=[[NSString alloc] initWithFormat:@"ijk"];
    }   

    lab.text=str;
    [str release];
}

output:

warning:unused variable str;  
  • 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-12T11:58:11+00:00Added an answer on May 12, 2026 at 11:58 am

    The problem is that in each ‘then’ clause of the various if statements, you’re creating a new local variable named str, assigning it to a new string, and then the variable goes out of scope. The compiler warning should tick you off to this: you’re writing to a variable but never reading from it.

    Ordinarily, your code wouldn’t compile, but you apparently have another variable named str in scope later on. Your new definitions of str are shadowing the old one: while the new name str is in scope, the name str refers to that variable, not the outer one, and the outer one is cannot be referred to.

    The solution is to move the declaration of str up to the top of the function. Furthermore, it’s simpler just to use [NSString stringWithFormat:@"blah"] instead of [[NSString alloc] initWithFormat:@"blah"], since the former gives you an autoreleased object. This saves you from having to manually release it later on. Note that assigning lab.text=str retains it, since the text property of the UILabel class has the retain modifier.

    -(IBAction)buttonclick:(id)sender  
    {  
        NSString *title=[sender titleForState:UIControlStateNormal];
        NSString *str;
    
        if([title isEqualToString:@"hello"])
        {
            str=[NSString stringWithFormat:@"abc"];
        }
        else if([title isEqualToString:@"nothing"])
        {
            str=[NSString stringWithFormat:@"def"];
        }
        else if([title isEqualToString:@"heaven"])
        {
            str=[NSString stringWithFormat:@"ijk"];
        }
    
        lab.text=str;
    }
    

    Also note that with your original code, you had both a memory leak and memory corruption — since you were allocating a string and then losing a reference to it (by the new local variable str going out of scope) without releasing it, and then you were calling release an extra time on whatever the outer str variable was. Moving the str declaration to the top of the function fixes both problems.

    I’m also assuming that your format strings are more complicated than just plain strings. If you’re actually assigning constant strings such as "abc", then of course it’s much simpler to just do str=@"abc" instead of str=[NSString stringWithFormat:@"abc"].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one label field and three buttons with the name of red, yellow,
I have two array and three button in segment control i want that when
I have three HTML objects and want to arrange Previous button at the left
I have three tabs. When I press the tab button on the bottom, is
I am devoloping a roof calculater. i have three edittext and calculate button in
I have a view with a button and three UITextFields. The view's background is
I have a group of three radio buttons. Depending on which radio button is
I have a parent FragmentActivity with three child Tab Fragments. When a Submit button
everyone, I have faced one problem: The ZUL code: <window title=Hello World!! id=winMain border=none
I have three inputs. I want them all to start and end with the

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.