如何为两个按钮创建不同的uiactionsheet?

时间:2011-10-06 10:04:37

标签: iphone

您好我在这里开发小型应用程序。在子类屏幕中,我有两个按钮。当我按下第一个按钮时,它将显示四个动作表。当我按下第二个按钮时,它将显示五个动作表。我成功地展示了它。但我不能设置五个动作表的第二个按钮动作。在我的代码中,当我按下第一个动作表的第二个按钮时,它将执行第一个动作表的第一个按钮。在这里,我想为各个动作表设置动作。请帮助我。这是我的代码是

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ( indexPath.row == 0)
{
    UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Edit" otherButtonTitles:@"Remove", @"Sell",@"Scrap", nil];
    popupQuery.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    [popupQuery showInView:self.view];
    [popupQuery release];
}
if ( indexPath.row == 1 ) 
{
    UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Unsell" otherButtonTitles:@"Edit Item", @"Edit Sale",@"Sold",@"Scrap", nil];
    popupQuery.tag=5;
    popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [popupQuery showInView:self.view];
    [popupQuery release];
}
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) 
{
    Updateasset *object=[[Updateasset alloc]initWithNibName:@"Updateasset" bundle:nil];
    [self presentModalViewController:object animated:NO];
    [object release];
} 
else if (buttonIndex == 1)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Remove" message:@"Do you want to Remove"
                                                   delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alert show];
    [alert release];
} 
else if (buttonIndex == 2)
{
    Egarageselling *object=[[Egarageselling alloc]initWithNibName:@"Egarageselling" bundle:nil];
    [self presentModalViewController:object animated:YES];
    [object release];
} 
else if (buttonIndex == 3) 
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Scrap" message:@"Do you want to Scrap"
                                                   delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alert show];
    [alert release];
}
else if (buttonIndex == 4) 
{

}
else if (buttonIndex == 5) 
{

}
else if (buttonIndex == 6) 
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Scrap" message:@"Do you want to Scrap"
                                                   delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alert show];
    [alert release];
}
else if (buttonIndex == 7) 
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Scrap" message:@"Do you want to Scrap"
                                                   delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alert show];
    [alert release];
}
else if (buttonIndex == 8) 
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Scrap" message:@"Do you want to Scrap"
                                                   delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alert show];
    [alert release];
}
else if (buttonIndex == 9) 
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Scrap" message:@"Do you want to Scrap"
                                                   delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alert show];
    [alert release];
}
}

如何解决这个问题。

2 个答案:

答案 0 :(得分:3)

为两个动作表设置不同的标签 popupQuery.tag = 5; popupQuery.tag = 6;

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    if(actionSheet.tag==5)
    {
       if (buttonIndex == 0) 
       {
       }
      so on
    }
    if(actionSheet.tag==6)
    {
       if (buttonIndex == 0) 
       {
       }
       so on
    }

 }

答案 1 :(得分:0)

我可能只是补充一点,在这样的情况下,我会说最佳做法是将您的操作表制作成公共或私有实例变量。它在您的头文件中看起来像这样(对于公共):

@property (nonatomic, retain) UIActionSheet *as1;
@property (nonatomic, retain) UIActionSheet *as2;

在您的实现文件中,您可以合成它们,如下所示:

@synthesize as1, as2;

然后记得从现在分配popupQuery的地方设置它们:

...
self.as1 = popupQuery;
...

...
self.as2 = popupQuery;
...

在你的委托方法中,你现在可以这样做(如果我应该为其他开发人员提供代码,我也会发现它更具可读性):

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    if(self.as1 == actionSheet) {
        if (buttonIndex == 0) { ... }
        ...
    }

    if(self.as2 == actionSheet) {
        if (buttonIndex == 0) { ... }
        ...
    }
}

我希望你在这个和许多其他案例中发现它很有用。我自己,一直这样做。对您提出公开或私人参考对象无害。你永远不知道他们什么时候派上用场。