我有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];
}
}
我在哪裏添加屬性或字段。在我的.m或.h下,我是否將這個添加到.m下的按鈕的IBAction中,並將這個void添加到每個按鈕語句中?對不起,提出了一些問題。謝謝你的時間。 – 2011-03-28 04:09:00
您可以像創建其他字段一樣創建字段(即在.h的界面中)。所有你需要做的是'selectedButton =(UIButton *)sender;'如果按鈕的動作。您將直接顯示UIActionSheet,並且單擊按鈕存儲在selectedButton中。 – NRaf 2011-03-28 04:20:25
如果您在Interface Builder中設置按鈕,那麼您應該在IBAction中設置按鈕。您不需要添加無效,您可以將其保留爲IBAction。 – NRaf 2011-03-28 04:22:14