【问题标题】:Find UIButton in Tableview header and change button title在 Tableview 标题中找到 UIButton 并更改按钮标题
【发布时间】:2020-03-22 14:42:36
【问题描述】:

我有两个部分的标题,每个标题都有 UIButton,当我点击标题中的 UIButtons 时,将打开一个弹出窗口。它有两个按钮是和否。当用户单击该弹出窗口中的“是”选项时,我想在“标题”按钮中显示“是”标题。 例如:如果用户点击第 1 节标题按钮弹出将打开并选择是。我想在我的第 1 节标题按钮图块中显示 Y$S 文本。

我的代码:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 0) {

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
         view.backgroundColor = [UIColor whiteColor];

         UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, tableView.frame.size.width-40, 21)];
         label.textAlignment = NSTextAlignmentLeft;

         label.font = [UIFont fontWithName:kAppFontLight size:12];
         label.textColor = [UIColor LSBlackColor];
         NSString *string = NSLocalizedString(@"Button1", nil);
         label.text = string;
         UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 48, tableView.frame.size.width, 1)];
         line.backgroundColor = [UIColor groupTableViewBackgroundColor];
 operate = [[UIButton alloc] initWithFrame:CGRectMake(tableView.frame.size.width - 100, 6, 100, 30)];
 [operate setTitle:@"Button1" forState:UIControlStateNormal];
 [operate setTitleColor:[UIColor colorWithRed: 65/255.0 green: 190/255.0 blue: 236/255.0 alpha:1] forState:UIControlStateNormal];
 operate.titleLabel.font = [UIFont fontWithName:kAppFontRegular size:14];
 operate.tag = 10;
 [operate addTarget:self action:@selector(operateAction:) forControlEvents:UIControlEventTouchUpInside];

         [view addSubview:label];
         [view addSubview:line];
         [view addSubview:operate];
         return view;
}
else if (section == 1) {
 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
         view.backgroundColor = [UIColor whiteColor];

         UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, tableView.frame.size.width-40, 21)];
         label.textAlignment = NSTextAlignmentLeft;

         label.font = [UIFont fontWithName:kAppFontLight size:12];
         label.textColor = [UIColor LSBlackColor];
         NSString *string = NSLocalizedString(@"Button1", nil);
         label.text = string;
 UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 48, tableView.frame.size.width, 1)];
    line.backgroundColor = [UIColor groupTableViewBackgroundColor];
    operateandEdit = [[UIButton alloc] initWithFrame:CGRectMake(tableView.frame.size.width - 120, 6, 100, 30)];
    [operateandEdit setTitle:@"Button2" forState:UIControlStateNormal];
    [operateandEdit setTitleColor:[UIColor colorWithRed: 65/255.0 green: 190/255.0 blue: 236/255.0 alpha:1] forState:UIControlStateNormal];
    operateandEdit.titleLabel.font = [UIFont fontWithName:kAppFontRegular size:14];
    operateandEdit.tag = 11;
    [operateandEdit addTarget:self action:@selector(operateAction:) forControlEvents:UIControlEventTouchUpInside];

         [view addSubview:label];
         [view addSubview:line];
         [view addSubview:operateandEdit];
         return view;
}
return nil;
}

在操作中:

-(void) operateAction:(id) sender {
if (operate.tag == 10) {
    if (sender.tag == 5) {
        [operate setTitle:@"YES" forState:UIControlStateNormal];
    } else {
        [operate setTitle:@"NO" forState:UIControlStateNormal];
    }
} else {
    if (sender.tag == 5) {
        [operateandEdit setTitle:@"YES" forState:UIControlStateNormal];
    } else {
        [operateandEdit setTitle:@"NO" forState:UIControlStateNormal];
    }
}
[self cancelButtonClicked:@""];
NSLog(@"%@", sender.currentTitle);
}

【问题讨论】:

    标签: ios objective-c uitableview cocoa-touch


    【解决方案1】:

    看:你的 operate.tag 将永远是 10 并且条件永远是 true 所以永远不要进入 else 块。

    检查以下代码,这里你必须为所有四个按钮取一个不同的标签,然后使用它。

    if (sender.tag == 5) {
        [operate setTitle:@"YES" forState:UIControlStateNormal];
    } else if (sender.tag == 6) {
        [operate setTitle:@"NO" forState:UIControlStateNormal];
    } else if (sender.tag == 7) {
        [operateandEdit setTitle:@"YES" forState:UIControlStateNormal];
    } else {
        [operateandEdit setTitle:@"NO" forState:UIControlStateNormal];
    }
    

    【讨论】:

      【解决方案2】:

      像这样快速更改按钮标题

      yourButton.setTitle("Yes", for: .normal)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-19
        • 2012-02-16
        • 2021-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-01
        • 1970-01-01
        相关资源
        最近更新 更多