【问题标题】:Radio button in iPhone appiPhone 应用程序中的单选按钮
【发布时间】:2009-03-19 15:28:51
【问题描述】:

我必须在我的 iPhone 应用程序中添加单选按钮。为此,我有圆形的单选按钮图像。我可以将该图像添加为子视图吗?我可以将 touchupinside 事件添加到 UIImage 吗?

【问题讨论】:

  • 我试过你说的,效果很好。最初按钮看起来像一个圆形按钮,但在单击按钮的那一刻,按钮的框架出现(看起来像一个方形按钮)

标签: iphone


【解决方案1】:

iPhone 应用程序没有单选按钮。执行此操作的“类似 iPhone”的方法是使用选取器视图或在分组表视图中包含几行,并在所选行上带有检查附件。

如果出于某种原因您真的觉得需要单选按钮,请使用带有自定义按钮类型的 UIButton。将空心圆圈设置为“正常”控制状态的图像,将实心圆圈设置为“选中”状态的图像。

【讨论】:

  • 我试过你说的,效果很好。最初按钮看起来像一个圆形按钮,但在单击按钮的那一刻,按钮的框架出现(看起来像一个方形按钮)
  • 我怎样才能摆脱这个东西?
  • 如果这个不可行,我必须尝试其他方法。所以请告诉我是否可以摆脱这种行为
  • 您还需要更改突出显示状态的图像。它可能应该是 Selected 按钮的变体,表明您正在按下按钮。
  • 要支持 Brent 对此答案的观点,请参阅此处的 Apple 的 iOS 人机界面指南,他们建议不要使用单选按钮来支持列表、选择器或切换控件:developer.apple.com/library/ios/#documentation/UserExperience/…
【解决方案2】:

试试UISegmentedControl。它的行为类似于单选按钮——呈现一系列选项并让用户选择 1。

【讨论】:

  • 你知道如何在 Titanium 中创建 UISegmentedControl 吗?
【解决方案3】:
IBOutlet UIButton *checkboxButtonMale; 

IBOutlet UIButton    *checkboxButtonFmale;

  checkboxButtonFmale.tag = 3;
  checkboxButtonMale.tag = 2; 

(IBAction)checkboxGenderChecked:(id)sender{ 


    UIButton *selectedButton = (UIButton *)sender;
    UIButton    *toggledButton; 

    if (selectedButton.tag == 2 ) { 
        toggledButton =    checkboxButtonFmale;
    } 
    else {      
        toggledButton = checkboxButtonMale; 
    }   [toggledButton    setSelected:NO]; 
    [sender setSelected:YES];
       }

【讨论】:

    【解决方案4】:

    您还可以创建一个 UIWebView,将使用单选按钮的 HTML 加载到其中,然后使用 UIWebView 委托方法,监控单选值。

    这是一种 hack,但在某些情况下,这正是我们需要的。

    【讨论】:

      【解决方案5】:

      您可以使用UIButtons 的数组,然后使用我的GSRadioButtonSetController 类来处理逻辑(本质上,确保只能选择一个按钮)。有关示例用法,请参阅 GitHub 上的 README。

      【讨论】:

        【解决方案6】:

        您可以尝试使用 UIImageView 类,该类仅将 UIImage 包装在 UIView 容器中。它支持我认为合适的 touchesBegan/Cancelled 事件。

        您也可以考虑不使用您所描述的真正的单选按钮设置,而是使用更类似于 iPhone 的方法 - 例如,使用 UITableView 与您的单选选项对应的单元格并选中切换/未经检查的触摸。有关示例,请参阅 Apple iPhone 开发网站上的 TouchCells 示例。

        【讨论】:

        • 我通过扩展 UIButton 类创建了我的单选按钮类,在我的单选按钮类中,如何将按钮类型设置为 UIButtonTypeCustom?。
        • 自定义是默认按钮类型。
        • 你好,我终于按照你的建议做了。我使用了 UIImageView 类和 touchesBegan 事件。它工作正常,没有任何问题。非常感谢。也感谢布伦特给我的想法。
        【解决方案7】:

        iPhone 应用程序中没有任何 RadioButton 控件。

        虽然您可以查看以下链接以获取 RadioButton。

        这里我们放置了两张图片来代替 RadioButtons。

        http://www.developers-life.com/radio-buttons-in-iphone-application.html

        享受编码........

        【讨论】:

          【解决方案8】:

          如果您只有“是”或“否”选项,请使用 UISwitch。

          对于多种类型,请使用以下代码:

          -(void)buttonPressed:(id)sender{
          
          if([sender isSelectd])
          
          //here change button image and set
          
          [sender setSelected:NO];
          
          }else{
          //here change button image and set
          
          [sender setSelected:YES];
          
          }
          

          记住,创建按钮时不要忘记将 setSelected 设置为 NO。

          【讨论】:

            【解决方案9】:

            我做了这样的事情并且它有效:

            - (IBAction)tickbuttonClicked:(id)sender
            {
            
            UIButton *selectedButton = sender;
            UIImage *ticked   = [UIImage imageNamed:@"tickbox_yes3.png"];   
            UIImage *unticked = [UIImage imageNamed:@"tickbox_no3.png"];    
            
                //untick active box
                if((activetag>-1)&&(activetag!=[sender tag])){
                    UIButton *thisButton = (UIButton *)[self.view viewWithTag:activetag];
                    thisButton.backgroundColor  = [UIColor colorWithPatternImage:unticked];
                }
            
             //tick sender
             if ([sender tag]!=activetag)
             {
                selectedButton.backgroundColor = [UIColor colorWithPatternImage:ticked]; 
                activetag=[sender tag];   
                [selectedButton setSelected:YES];
            
             }
            
            }
            

            【讨论】:

              【解决方案10】:

              //定义按钮属性

              声明未更改的图像

                 - (void)viewDidLoad
                 {
                     [ButtonChaked setBackgroundImage:[UIImage imageNamed:@"UnChacked.png"] forState:UIControlStateNormal];
                  }
              

              查过的图片

               - (IBAction)ButtonChaked:(id)sender
               {
                  if (ButtonChaked.selected ==YES)
                  {
                    [ButtonChaked setBackgroundImage:[UIImage imageNamed:@"UnChacked.png"] forState:UIControlStateSelected];
              
                  ButtonChaked.selected =NO;
              
               }
               else
               {
                  [ButtonChaked setBackgroundImage:[UIImage imageNamed:@"Chaked.png"] forState:UIControlStateNormal];
                  ButtonChaked.selected = YES;
              
               }
              
              
              }
              

              真的用过

              【讨论】:

                【解决方案11】:

                我刚刚在 Cocoapods pod "DLRadioButton" 上发布了一个 iOS Radio Button 控件,有关文档和示例,请转到 GitHub。附上这个控件的截图。

                【讨论】:

                  最近更新 更多