警报视图方法不起作用

时间:2011-09-13 06:05:23

标签: iphone xcode uialertview

在我的应用程序中,我有重置按钮,删除表和数据库的所有数据。但在删除之前,我必须提出警报视图,并提出如下问题:

- (IBAction)resetData:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Bills Data Entry"
                      message:@"Are you sure want to reset data?" delegate:nil
                      cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alert show];
    [alert release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {

        [self databaseOpen];
        NSString *deleteData=[NSString stringWithFormat:@"delete from tbl_Bills"];
        [database executeQuery:deleteData];
        NSLog(@"inert query: %@",deleteData);
        NSLog(@"records deleted");
        [table reloadData];
        [database close];

        // DO STUFF
    }
}

点击“是”时,不会调用此方法。

3 个答案:

答案 0 :(得分:4)

如果要调用其委托方法,则UIAlertView的委托不能为nil。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Bills Data Entry"
                        message:@"Are you sure want to reset data?" delegate:self
                        cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];

答案 1 :(得分:1)

如果要调用其委托方法,则必须将UIAlertView委托设置为self。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Bills Data Entry"
                                          message:@"Are you sure want to reset data?"
                                          delegate:self
                                          cancelButtonTitle:@"No"
                                          otherButtonTitles:@"Yes",nil];

答案 2 :(得分:1)

  When you are using Delegate method you are supposed to set "delegate: self" instead of nil.

  UIAlertView *my_Alert = [[UIAlert alloc] initWithTitle:@"Title of Alert" message:@"Your 
                          Message" delegate:self cancelButtonTitle:@"OK", 
                          otherButtonTitles:nil,nil];