2011-10-06 32 views
0

嗨,我在這裏開發小應用程序。在子類屏幕中,我有兩個按鈕。當我按下第一個按鈕時,它會顯示四個操作表。當我按下第二個按鈕時,它將顯示五個操作表。我成功地展示了它。但我不能設置五個動作表的第二個按鈕動作。在我的代碼中,當我按下第一個動作表的第二個按鈕時,它將啓動第一個動作表的第一個按鈕。在這裏,我想爲各個操作表設置操作。請幫助我。這裏是我的代碼是如何爲兩個按鈕創建不同的uiactionsheet?

- (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]; 
} 
} 

如何解決這個問題。

回答

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 
    } 

} 
+0

它會工作,但它也動作與第一個按鈕的動作表如何解決 –

+0

我沒有得到你的觀點 – Narayana

+0

這是我的代碼。 如果(indexPath.row == 0) \t { \t \t ... \t \t popupQuery.tag = 5; \t \t ..... \t} \t如果(indexPath.row == 1) \t { \t \t .... \t \t popupQuery1.tag = 6; \t \t ... \t} - (空)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger的)buttonIndex { \t \t如果(actionSheet。標籤= 5) \t { \t \t如果(buttonIndex == 0) \t \t { \t \t} \t} \t \t 如果(actionSheet.tag = 6) \t { \t \t如果(buttonIndex = = 0) \t \t { \t \t \t \t \t} \t \t等等 } –

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) { ... } 
     ... 
    } 
} 

我希望你覺得它在這個和許多其他情況下很有用。我自己,一直這樣做。無論是公開還是私人蔘考,都不會對您造成任何損害。你永遠不知道他們什麼時候可以派上用場。

相關問題