UIActionSheet使多个UIButton变化
问题描述:
我有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];
}
}
答
鉴于格式化,您的代码有点难以阅读,但如果我理解正确,您可以尝试以下操作。
首先,创建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将指向正确的按钮,所以它应该是相当简单的根据需要进行修改。
答
使用默认按钮创建UIActionSheet。在每个按钮点击上设置您需要在actionSheet中显示的按钮标题。
for (int i = 0; i < itemCount; i++) {
[actionSheet addButtonWithTitle:itemText];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
我在哪里添加属性或字段。在我的.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