如何保持 iPhone 按钮的选定状态

发布于 2024-09-10 08:05:18 字数 1147 浏览 5 评论 0原文

我创建了滚动视图并将按钮设置在滚动视图中。按钮水平滚动并且工作正常。如果我单击该按钮,我会将背景图像设置为按钮中的“选定状态”。我的问题是,单击另一个按钮时如何更改不同按钮中的选定状态,以及单击另一个按钮时如何取消选择“选定状态”按钮。

我在滚动视图中有三个按钮,

 -(IBAction) Button1 : (id) sender
 {  
    // btn1.selected = YES;
          [btn1 setImage:[UIImage imageNamed:@"first.png"]  forState:UIControlStateSelected];

 }

 -(IBAction) Button2 : (id) sender
 {  
    // btn2.selected = YES;
        [btn2 setImage:[UIImage imageNamed:@"second.png"]  forState:UIControlStateSelected];

 }

 -(IBAction) Button3 : (id) sender
 {
   // btn3.selected = YES;
       [btn3 setImage:[UIImage imageNamed:@"three.png"]  forState:UIControlStateSelected];

 }

请参见下图,(健康、娱乐和理财是三个按钮)

图片 http://www.freeimagehosting.net/uploads/6b3daab12f.png

Img http://www.freeimagehosting.net/uploads/b6e0f234dc.png

注意:(如选项卡栏和分段控件)

单击第一个按钮并将背景图像设置为选定状态并单击第二个按钮时,那么第一个按钮将被取消选择。那么我怎样才能保持选定的状态,直到单击另一个按钮。

提前致谢。

I have created scroll view and sets the buttons are in the scroll view. The Buttons are scrolling horizontally and it works fine. If i clicked the button, i set background image as "Selected State" in button. My problem is how can i changed the selected state in different button, when clicking it and how can i deselected the "selected state" button when clicking the another button.

I have three buttons in the scroll view,

 -(IBAction) Button1 : (id) sender
 {  
    // btn1.selected = YES;
          [btn1 setImage:[UIImage imageNamed:@"first.png"]  forState:UIControlStateSelected];

 }

 -(IBAction) Button2 : (id) sender
 {  
    // btn2.selected = YES;
        [btn2 setImage:[UIImage imageNamed:@"second.png"]  forState:UIControlStateSelected];

 }

 -(IBAction) Button3 : (id) sender
 {
   // btn3.selected = YES;
       [btn3 setImage:[UIImage imageNamed:@"three.png"]  forState:UIControlStateSelected];

 }

see the below image,(Health, Entertainment and Money Watch are the three buttons)

Image http://www.freeimagehosting.net/uploads/6b3daab12f.png

and

Img http://www.freeimagehosting.net/uploads/b6e0f234dc.png

Note:(Like, Tabbar and Segmented control)

On clicking first button and sets background image in selected state and clicking the second button, then first buttons are to be deselected. So how can i maintain the selected state, till another button is clicked.

Thanks in Advance.

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

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

发布评论

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

评论(1

傲世九天 2024-09-17 08:05:18

我通过以下方式解决了此任务:

init 方法:

  • 使用为正常状态和选定状态定义的图像创建多个按钮。
  • 为每个按钮分配标签(例如,第 i 个按钮标签为 1000+i)。
  • 为每个按钮分配 IBAction。

操作方法:

  • 从先前选择的按钮中删除选择(使用 [view viewWithTag:] 方法通过其标签进行搜索)
  • 选择发件人。
  • 保存发件人的标签。

这是代码:

- (void)init {

....INITIALIZE SCROLLVIEW HERE.....

    for ( int i = 0; i < 10; i++ ) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        [btn setFrame:.....];
        [btn setImage:_IMAGE_ forState:UIControlStateNormal];
        [btn setImage:_IMAGE2_ forState:UIControlStateSelected];
        [btn setTag:i + 1000];

        [btn addTarget:self action:@selector(setSelectedButton:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:btn];
    }
}

- (IBAction)setSelectedButton:(id)sender {
    [self setSelectedButtonByIndex:((UIButton *)sender).tag - 1000];
}

- (void)setSelectedButtonByIndex:(NSInteger)index {
    if ( selectedElemId >= 0 ) {
        UIButton *btn = (UIButton *)[self viewWithTag:selectedElemId + 1000];
        [btn setSelected:NO];
    }

    UIButton *btn = (UIButton *)[self viewWithTag:index + 1000];
    [btn setSelected:YES];

    selectedElemId = btn.tag - 1000;
}

I solved this task in the following way:

init method:

  • Create number of buttons with defined images for normal and selected state.
  • Assign tag for each button (for example, for i'th button tag is 1000+i).
  • Assign IBAction for each button.

action method:

  • Remove selection from previously selected button (search it by it's tag with [view viewWithTag:] method)
  • Select sender.
  • Save sender's tag.

Here's the code:

- (void)init {

....INITIALIZE SCROLLVIEW HERE.....

    for ( int i = 0; i < 10; i++ ) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        [btn setFrame:.....];
        [btn setImage:_IMAGE_ forState:UIControlStateNormal];
        [btn setImage:_IMAGE2_ forState:UIControlStateSelected];
        [btn setTag:i + 1000];

        [btn addTarget:self action:@selector(setSelectedButton:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:btn];
    }
}

- (IBAction)setSelectedButton:(id)sender {
    [self setSelectedButtonByIndex:((UIButton *)sender).tag - 1000];
}

- (void)setSelectedButtonByIndex:(NSInteger)index {
    if ( selectedElemId >= 0 ) {
        UIButton *btn = (UIButton *)[self viewWithTag:selectedElemId + 1000];
        [btn setSelected:NO];
    }

    UIButton *btn = (UIButton *)[self viewWithTag:index + 1000];
    [btn setSelected:YES];

    selectedElemId = btn.tag - 1000;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文