UIActionSheet进行多个UIButton更改

时间:2011-03-27 01:13:55

标签: iphone ios sdk uiactionsheet

我在视图上有6个UI按钮。我试图为每个按钮调用一个UIActionSheet,并更改按钮图像和UIlabel。我可以按照需要让第一个工作。我似乎无法让第二个像第一个那样进行更改。 这是我的代码。请帮忙。

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

2 个答案:

答案 0 :(得分:1)

使用默认按钮创建UIActionSheet。在每个按钮上点击设置您需要在actionSheet中显示的按钮标题。

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

答案 1 :(得分: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将指向正确的按钮,因此根据需要修改它应该非常简单。