actionSheet clickedButtonAtIndex在目标c中不起作用?

时间:2012-01-23 12:54:48

标签: objective-c ios uibutton uiactionsheet

我尝试单击按钮,从操作表中选择,然后将所选索引设置为按钮文本标签的文本。我的按钮在下面。但它不会在按钮文本中显示所选索引。我怎么解决这个问题?

- (IBAction)cinsiyetBtnClick:(id)sender {
    popupQuery1 = [[UIActionSheet alloc] initWithTitle:@"Cinsiyetiniz" 
                                              delegate:self 
                                     cancelButtonTitle:NO 
                                destructiveButtonTitle:NO 
                                     otherButtonTitles:@"Bay", @"Bayan",nil];
    popupQuery1.tag = 1;
    //    popupQuery1.actionSheetStyle = UIActionSheetStyleDefault;
    [popupQuery1 showInView:[UIApplication sharedApplication].keyWindow];
    [popupQuery1 release];
}

- (IBAction)medeniDurumBtnClick:(id)sender {
    popupQuery1 = [[UIActionSheet alloc] initWithTitle:@"Medeni Durumunuz" 
                                              delegate:self 
                                     cancelButtonTitle:NO 
                                destructiveButtonTitle:NO 
                                     otherButtonTitles:@"Evli", @"Bekar",@"Dul",@"Boşanmış",nil];
    popupQuery1.tag = 2;
    //    popupQuery1.actionSheetStyle = UIActionSheetStyleDefault;
    [popupQuery1 showInView:[UIApplication sharedApplication].keyWindow];
    [popupQuery1 release];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(actionSheet.tag==1){
        if (buttonIndex == 0) {
            self.cinsiyetBtn.titleLabel.text = @"Bay";
        } else if (buttonIndex == 1) {
            self.cinsiyetBtn.titleLabel.text = @"Bayan";
        } 
    }
    else if (actionSheet.tag == 2){
        if (buttonIndex == 0) {
            self.medeniDurumBtn.titleLabel.text = @"Evli";
        } else if (buttonIndex == 1) {
            self.medeniDurumBtn.titleLabel.text = @"Bekar";
        } else if (buttonIndex == 2) {
            self.medeniDurumBtn.titleLabel.text = @"Dul";
        } else if (buttonIndex == 3) {
            self.medeniDurumBtn.titleLabel.text = @"Boşanmış";
        } 
    }
}

1 个答案:

答案 0 :(得分:1)

有几种状态可以是按钮:UIControlStateNormalUIControlStateHighlightedUIControlStateSelectedUIControlStateDisabled

在大多数情况下,当您想要更新按钮的标题时,您应该为正常状态设置新标题:

[button setTitle:@"New Title" forState:UIControlStateNormal];

有时您需要为所有州设置标题。

所以现在你应该用这样的东西替换你所有的self.cinsiyetBtn.titleLabel.text = @"Bay";

[self.cinsiyetBtn setTitle:@"Bay" forState:UIControlStateNormal];