tags:

views:

605

answers:

2

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;
+3  A: 

Don't use the title of the buttons to differentiate between your buttons. It wouldn't work if your buttons were to be localised. Either use different actions, or use the tag to differentiate them.

The warning is the clue to what you're doing wrong in this case. A local variable is only visible within the scope that it's declared, so your lab.text=str line is actually setting lab.text to a str that is defined elsewhere, either a static variable or an instance variable. Here's what you could do instead:

NSString *str;

switch ([sender tag]) {
  case FirstButtonTag:
    str = @"abc";
    break;
  case SecondButtonTag:
    str = @"def";
    break;
  case ThirdButtonTag:
    str = @"ijk";
    break;
}

lab.text = str;
Chris Suter
+2  A: 

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"].

Adam Rosenfield