iOS,目标C,通过标签引用按钮

曼努埃尔·贝坦科特(Manuel Betancurt)

我有一些根据数组创建的按钮

for (NSString *buttonTitle in buttons) {
    self.bottomOptionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.bottomOptionButton.frame = CGRectMake(xPos , buttonHeight , buttonWidth, buttonHeight);
    [self.bottomOptionButton setTitle:buttonTitle forState:UIControlStateNormal];
    [self.bottomOptionButton setTitleColor:[[ThemeManager shared]slipKeyboardFontColor] forState:UIControlStateNormal];
    self.bottomOptionButton.tag = index;
    [self.bottomOptionButton addTarget:self action:@selector(optionBottomButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.optionsKeyboard addSubview:self.bottomOptionButton];

    xPos += buttonWidth;
    index += 1;

    UIView *lowerVerticalLine = [[UIView alloc]initWithFrame:CGRectMake(xPos, buttonHeight, lineThickness, buttonHeight)];
    lowerVerticalLine.backgroundColor = [[ThemeManager shared]separatorLineColor];
    [self.optionsKeyboard addSubview:lowerVerticalLine];
}

它们被创建并显示良好,现在,当点击一个按钮时,我可以获取该按钮所引用的标签,但是我需要通过尚不起作用的标签来设置按钮的背景色

- (void)optionBottomButtonPressed:(id)sender
{
    UIButton *button = sender;

    NSLog(@"tu tag :: %d", button.tag);
    NSLog(@"de opcion :: %@", [self.bottomButtonArray objectAtIndex:button.tag] );

    UIColor *selectedButtonColor = [[ThemeManager shared]buttonSelectedBackground];
    UIColor *nonSelectedButtonColor = [UIColor whiteColor];

    for (int i = 0; i<= self.bottomButtonArray.count; i++) {

        UIButton *button = (UIButton *)[self.bottomOptionButton viewWithTag:i];
        button.backgroundColor = nonSelectedButtonColor;
    }

    [sender setBackgroundColor:[UIColor redColor]];

}

那么如何通过标签更改按钮的背景呢?

谢谢!

Akhilrajtr

试试这个,

for (NSString *buttonTitle in buttons) {
    UIButton *bottomOptionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    ....
    bottomOptionButton.tag = index;
    ....
    [self.optionsKeyboard addSubview:bottomOptionButton];

    ....
}

而在 optionBottomButtonPressed:

- (void)optionBottomButtonPressed:(id)sender
{
     NSLog(@"tu tag :: %d", button.tag);
     NSLog(@"de opcion :: %@", [self.bottomButtonArray objectAtIndex:button.tag] );

     UIColor *selectedButtonColor = [[ThemeManager shared]buttonSelectedBackground];
     UIColor *nonSelectedButtonColor = [UIColor whiteColor];

     for (int i = 0; i<= self.bottomButtonArray.count; i++) {

         UIButton *button = (UIButton *)[self.optionsKeyboard viewWithTag:i];
         button.backgroundColor = nonSelectedButtonColor;
     }

     [sender setBackgroundColor:[UIColor redColor]];

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

通过单击按钮 GTK C 更改标签

来自分类Dev

iOS目标C用按钮替换uitabbar但按钮没有交互?

来自分类Dev

访问 cellForRowAtIndexPath 之外的单元格标签,即按钮操作目标 C

来自分类Dev

:mappedBy通过引用未知的目标实体属性

来自分类Dev

JPA映射通过引用未知目标实体

来自分类Dev

Xcode,IOS,目标C

来自分类Dev

Xcode UI测试(目标C):通过辅助功能标签查找元素

来自分类Dev

如何在目标c中使用Realm.io通过主题标签过滤帖子?

来自分类Dev

目标C如何通过单击按钮为子视图制作动画

来自分类Dev

隐藏标签并显示按钮iOS

来自分类Dev

在iOS中创建标签按钮

来自分类Dev

iOS是否可以通过按一个按钮来更改按钮的图像并用文本填充标签?

来自分类Dev

目标C-在iOS SDK 8.4中删除后退按钮的默认V形字形

来自分类Dev

如何在iOS,目标c的didselectrowatindexpath中触发按钮操作方法

来自分类Dev

iOS /目标C:应用因核心数据而崩溃-按下保存按钮

来自分类Dev

通过引用返回C ++

来自分类Dev

C ++通过引用问题

来自分类Dev

通过元数据标签引用类

来自分类Dev

如何通过目标C代码在ios中更改通知消息?

来自分类Dev

如何在目标C中的ios中通过XMPPFramework连接XMPP服务器

来自分类Dev

通过单击按钮来引用li的跨度

来自分类Dev

使用数组目标C / iOS

来自分类Dev

目标-C多接口iOS

来自分类Dev

CSS:通过目标网址/ href引用链接项

来自分类Dev

如何通过API检索Gitlab项目标签列表?

来自分类Dev

按钮中的 C# 标签

来自分类Dev

按钮边框颜色-目标C-快速

来自分类Dev

目标C:“后退”按钮无法正常工作

来自分类Dev

目标 c 中的单选按钮问题

Related 相关文章

热门标签

归档