UIActionSheet making multiple UIButton changes
I have 6 UIbuttons on a view. I am trying to call a UIActionSheet for each button, and change the buttons image and a UIlabel. I can get the first one to work as desired. I can't seem to get the second to make the changes like the first. Here is my code. please help.
-(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.sce开发者_Python百科neLabel.text = @"PENDING";
[self.sceneButton setImage:[UIImage imageNamed:@"YellowButton.png"] forState:UIButtonTypeCustom];
self.sceneLabel.textColor = [UIColor blackColor];
}
}
Create UIActionSheet with Default Buttons. On every button tap set the button titles you need to show in actionSheet.
for (int i = 0; i < itemCount; i++) {
[actionSheet addButtonWithTitle:itemText];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
Given the formatting, your code is a bit difficult to read but if I understand you correctly, you could try the following.
First, create a selectedButton property or field. Add a target for each button:
[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
...
}
Then inside your UIActionSheet clickedButtonAtIndex: method, the selectedButton will be pointing towards the correct button, so it should be pretty simple to modify it as needed.
精彩评论