无法从UIAlertView导航到主页

时间:2012-08-30 06:17:02

标签: iphone objective-c uitableview uialertview

我正在使用UITableView来显示数据,并且通过使用自定义按钮和删除功能,我试图删除所选行。但我想在UITableView为空时在该函数内部放置一个alertview,并使用UIAlertView内的按钮,我试图根据条件导航到主页面和上一页面。但是在UITableView变空之后它就会崩溃,我按下“程序接收信号:”SIGABRT“的删除按钮。”

我的代码如下所示:

    - (IBAction)DeleteButtonAction:(id)sender
    {   
        DMSAppDelegate *d = (DMSAppDelegate *)[[UIApplication sharedApplication] delegate];

        NSLog(@"Message From Custom Cell Received");

        if(d->newtableData.count != 0)
        {

            NSIndexPath *indexPath = [self.tablevieww2 indexPathForCell:(UITableViewCell *)[[[sender superview] superview] superview]];
            NSUInteger row = [indexPath row];
            [d->newtableDataQt removeObjectAtIndex:row];
            NSLog(@"data removed");
            [self.tablevieww2 reloadData];
        }   
        else
        {       
            UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:@"hello" message:@"Warning!!: Table is empty" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"",@"No",nil];
            textfieldQty1 = [alertview textFieldAtIndex:0];
            textfieldQty1.keyboardType = UIKeyboardTypeNumberPad;
            textfieldQty1.keyboardAppearance = UIKeyboardAppearanceAlert;
            textfieldQty1.autocorrectionType = UITextAutocorrectionTypeNo;
            [alertview show];
            [alertview release];    
        }

    }

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    {
        if (buttonIndex == 0) 
        {
            DMSViewController *bt=[[DMSViewController alloc]initWithNibName:nil bundle:nil];
            bt.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
            [self presentModalViewController:bt animated:YES];
        }
        else if (buttonIndex == 1)
        {
            NSString *newqty = [[NSString alloc] initWithFormat:@"%@",textfieldQty1.text];
            DMSAppDelegate *d= (DMSAppDelegate *)[[UIApplication sharedApplication] delegate];
            [d->newtableDataQt replaceObjectAtIndex:slCell1 withObject:(id)newqty];
            NSLog(@"tb%@",d->newtableDataQt);
            [self.tablevieww2 reloadData];  
            int total1=0;

            for ( int i=0 ; i < [d->newtableDataQt count];++i )
            {           
                NSString *string = [d->newtableDataQt objectAtIndex:i];

                NSLog(@"string%@",string);

                if ([string isEqualToString:@"0"])
                {

                }
                else
                {
                    NSLog(@"newArray%@",d->newtableDataPrice);
                    NSString *strP=[d->tableDataPrice objectAtIndex:i];
                    NSInteger sp=[strP integerValue];
                    NSInteger st=[string integerValue];
                    total1=total1+st*sp;
                    NSLog(@"total1%d",total1);
                }
            }

            NSString *newtotal1=[NSString stringWithFormat:@"%d",total1];
            DMSAppDelegate *d2 = (DMSAppDelegate   *) [[UIApplication sharedApplication] delegate];
            d2->totalD = [[NSString alloc] initWithString:newtotal1];
        }   
    }

    Please give me some solution. I am trying really hard from yesterday but not getting any success.
    Thanks in advance.
    @

1 个答案:

答案 0 :(得分:1)

你需要检查两件事: -

首先: - if(d->newtableData.count != 0) 是条件,您不会从newtableData删除newtableDataQt中的项目,因此您的其他方法不会被调用。因为newtableData永远不会有count = 0。

第二件事; - 如果你的表是空的,那么就意味着newtableDataQt将不包含任何值,它将为空。当你点击删除按钮时,会出现警报视图,之后如果你点击索引1处的任何按钮,那么你写的代码: -

[d->newtableDataQt replaceObjectAtIndex:slCell1 withObject:(id)newqty];

所以newtableDataQt之前已经是空的,现在你正在使用它。这可能是崩溃的原因。

尝试

if( [newtableDataQt count] >slCell1)
{    [d->newtableDataQt replaceObjectAtIndex:slCell1 withObject:(id)newqty];
}

我希望它对你有所帮助。