2011-03-27 57 views
0

我有6個UIbuttons在視圖上。我正在嘗試爲每個按鈕調用UIActionSheet,並更改按鈕圖像和UIlabel。我可以得到第一個按需要工作。我似乎無法得到第二個像第一個那樣進行更改。 這是我的代碼。請幫忙。UIActionSheet使多個UIButton變化

-(IBAction)lifeStatus:(id)sender { 

    UIActionSheet *lifeStatus = [[UIActionSheet alloc] initWithTitle:@"Please Select" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"UNSECURED" otherButtonTitles:@"SECURED", @"PENDING", nil]; 


    lifeStatus.actionSheetStyle = UIActionSheetStyleBlackOpaque; 
    [lifeStatus showInView:self.view.window]; 
    [lifeStatus release]; 

} 
-(void)actionSheet:(UIActionSheet *)lifeStatus clickedButtonAtIndex:(NSInteger) buttonIndex { 
    if (buttonIndex == 0) { 
     self.lifeLabel.text = @"UNSECURED"; 
     [self.lifeButton setImage:[UIImage imageNamed:@"RedButton.png"] forState:UIButtonTypeCustom]; 
     self.lifeLabel.textColor = [UIColor whiteColor]; 
    } else if (buttonIndex == 1) { 
     self.lifeLabel.text = @"SECURED"; 
     [self.lifeButton setImage:[UIImage imageNamed:@"GreenButton.png"] forState:UIButtonTypeCustom]; 
     self.lifeLabel.textColor = [UIColor whiteColor]; 
    } else if (buttonIndex == 2) { 
     self.lifeLabel.text = @"PENDING"; 
     [self.lifeButton setImage:[UIImage imageNamed:@"YellowButton.png"] forState:UIButtonTypeCustom]; 
     self.lifeLabel.textColor = [UIColor blackColor]; 
    } 
} 

-(IBAction)sceneStatus:(id)sender { 

    UIActionSheet *sceneStatus = [[UIActionSheet alloc] initWithTitle:@"Please Select" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"UNSECURED" otherButtonTitles:@"SECURED", @"PENDING", nil]; 

    sceneStatus.actionSheetStyle = UIActionSheetStyleBlackOpaque; 
    [sceneStatus showInView:self.view.window]; 
    [sceneStatus release]; 

} 

-(void)actionSheet1:(UIActionSheet *)sceneStatus clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     self.sceneLabel.text = @"UNSECURED"; 
     [self.sceneButton setImage:[UIImage imageNamed:@"RedButton.png"] forState:UIButtonTypeCustom]; 
     self.sceneLabel.textColor = [UIColor whiteColor]; 
    } else if (buttonIndex == 1) { 
     self.sceneLabel.text = @"SECURED"; 
     [self.sceneButton setImage:[UIImage imageNamed:@"GreenButton.png"] forState:UIButtonTypeCustom]; 
     self.sceneLabel.textColor = [UIColor whiteColor]; 
    } else if (buttonIndex == 2) { 
     self.sceneLabel.text = @"PENDING"; 
     [self.sceneButton setImage:[UIImage imageNamed:@"YellowButton.png"] forState:UIButtonTypeCustom]; 
     self.sceneLabel.textColor = [UIColor blackColor]; 
    } 
} 

回答

0

鑑於格式化,您的代碼有點難以閱讀,但如果我理解正確,您可以嘗試以下操作。

首先,創建selectedButton屬性或字段。新增目標爲每個按鈕:

[button1 addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchDownOutside]; 
[button2 addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchDownOutside]; 
... other buttons ... 

-(void)buttonTouched:(id)sender { 
    self.selectedButton = (UIButton *)sender; 

    //Show UIActionSheet here 
    ... 
} 

然後你UIActionSheet clickedButtonAtIndex內:法,selectedButton將指向正確的按鈕,所以它應該是相當簡單的根據需要進行修改。

+0

我在哪裏添加屬性或字段。在我的.m或.h下,我是否將這個添加到.m下的按鈕的IBAction中,並將這個void添加到每個按鈕語句中?對不起,提出了一些問題。謝謝你的時間。 – 2011-03-28 04:09:00

+0

您可以像創建其他字段一樣創建字段(即在.h的界面中)。所有你需要做的是'selectedButton =(UIButton *)sender;'如果按鈕的動作。您將直接顯示UIActionSheet,並且單擊按鈕存儲在selectedButton中。 – NRaf 2011-03-28 04:20:25

+0

如果您在Interface Builder中設置按鈕,那麼您應該在IBAction中設置按鈕。您不需要添加無效,您可以將其保留爲IBAction。 – NRaf 2011-03-28 04:22:14

1

使用默認按鈕創建UIActionSheet。在每個按鈕點擊上設置您需要在actionSheet中顯示的按鈕標題。

for (int i = 0; i < itemCount; i++) { 
    [actionSheet addButtonWithTitle:itemText]; 
} 
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];