开发者

alterView text Issue!

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:

  1. You have unnecessary alloc/init calls with no corresponding call to release, so you are leaking memory.

  2. You are attempting to access the titleLabel of an object that you just allocated, instead of the titleLabel associated with the button that was pressed (which I assume is what you want). Depending upon what BeginingCell's init 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.

  3. 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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜