【问题标题】:Highlight the UIButtons created Dynamically?突出显示动态创建的 UIButtons?
【发布时间】:2012-01-22 13:21:37
【问题描述】:

在我的应用程序中,我在滚动视图中创建了 20 个按钮,现在的问题是我无法突出显示选定的按钮。

我的目的是让按下的按钮具有与正常不同的外观。当另一个按钮被按下时,前一个按钮应该变得正常:

UIButton *Abutton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    [Abutton setTag:i-1];
    Abutton.frame = CGRectMake(30.0, 0+j, 40.0, 40.0);
    [Abutton setTitle:@"" forState:UIControlStateNormal];
    Abutton.backgroundColor = [UIColor clearColor];
    [Abutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
    UIImage *buttonImageNormal = [UIImage imageNamed:@"image1.png"];
    UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [Abutton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
    UIImage *buttonImagePressed = [UIImage imageNamed:@"image2.png"];
    UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [Abutton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
    [Abutton addTarget:self action:@selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];

    [scrollview addSubview:Abutton];

最后我创建了Abutton按下的方法如下:

-(IBAction)buttonpressed:(id)sender{
         Abutton.highlighted=YES;
         //.....
         //.....
}

如果这样做,那么只有最后一个动态创建的按钮会被突出显示。这不是我想要的。

【问题讨论】:

    标签: ios ipad uiscrollview uibutton ios-simulator


    【解决方案1】:

    解决方案 1: 创建一个引用所有按钮的NSSet。在您的buttonPressed 方法中,在NSSet 的按钮上调用makeObjectsPerformSelector,将它们设置为未突出显示状态。

    解决方案 2: 使用 UISegmentedControl。无论如何,这似乎是您在这种情况下应该做的事情。

    【讨论】:

    • 谢谢 Ben,有没有更好的使用 NSSet 的例子?
    【解决方案2】:

    我认为您应该替换当前按下的按钮的代码:

        -(IBAction)buttonpressed:(id)sender{
    UIButton *b = (UIButton *)sender;
                 b.highlighted = YES;
                 //.....
                 //.....
        }
    

    在您的示例中,您特别突出显示“AButton”。此代码突出显示正在按下的按钮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多