actionSheet clickedButtonAtIndex 在 Objective C 中不起作用?

发布于 2024-12-28 17:10:01 字数 2035 浏览 1 评论 0原文

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

- (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ış";
        } 
    }
}

I try to click button, select from action sheet and then set the selected index to the button text label's text. My button is below. But it not show selected index in button text. How can I solve this?

- (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ış";
        } 
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老旧海报 2025-01-04 17:10:01

按钮可能处于多种状态:UIControlStateNormalUIControlStateHighlightedUIControlStateSelectedUIControlStateDisabled

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

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

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

因此,现在您应该将所有 self.cinsiyetBtn.titleLabel.text = @"Bay"; 替换为如下内容:

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

There are several states in which button could be: UIControlStateNormal, UIControlStateHighlighted, UIControlStateSelected and UIControlStateDisabled.

In most cases when you want to update title of button you should set new title for normal state:

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

Sometimes you would need to set titles for all the states.

So now you should just replace all your self.cinsiyetBtn.titleLabel.text = @"Bay"; with something like this:

[self.cinsiyetBtn setTitle:@"Bay" forState:UIControlStateNormal];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文