I want to show a button name on it's click that pop's up a alertView.I tried with the following code.But the titleLabel is not showing on the alertView.
Code:
-(void) btnAction:(id) sender
{
BeginingCell *cellObject;
cellObject=[[BeginingCell alloc]init];
NSString *str= [[NSString alloc] init];
str=cellObject.ansBtn1.titleLabel.text;
UIAlertView *alrt=[[UIAlertView alloc]initWi开发者_开发问答thTitle:str message:str delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil ];
[alrt show];
}
Can anyone tell me what's wrong with the above code?
There are a few of issues:
You have unnecessary alloc/init calls with no corresponding call to
release
, so you are leaking memory.You are attempting to access the
titleLabel
of an object that you just allocated, instead of thetitleLabel
associated with the button that was pressed (which I assume is what you want). Depending upon what BeginingCell'sinit
method does, you may get a nil string, a string with some default value, or even a crash due to attempting to access a field that has not been properly initialized.You're also not releasing your UIAlertView when you are done with it.
You may have better luck with something like:
-(void) btnAction:(id) sender {
NSString *str = ((UIButton*)sender).titleLabel.text;
UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:str message:str
delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil ];
[alrt show];
[alrt release];
}
精彩评论