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