iphone what to use as there are no “radio buttons”

2019-09-04 23:18发布

问题:

For the iPhone, I havent been able to find anything like a radio button that triggers the other buttons in a group. So what would everyone suggest using? UISwitch I guess and do something to trigger the others in the group when an other is selected?

If I had several UISwitch objects how would I trigger the others to be the opposite of what I switched?

回答1:

I also used a custom UISegmentedControl. Something like this:

NSMutableArray* buttons;

- (void)touchDownAction:(UIButton*)button {
    [self dimAllButtonsExcept:button];
    if ([delegate respondsToSelector:@selector(touchDownAtSegmentIndex:)])
        [delegate touchDownAtSegmentIndex:[buttons indexOfObject:button]];
}

-(void) dimAllButtonsExcept:(UIButton*)selectedButton {
    for (UIButton* button in buttons) {
        if (button == selectedButton) {
            button.selected = YES;
            button.highlighted = YES;
        } else {
            button.selected = NO;
            button.highlighted = NO;
        }
    }
}

The full code is at https://github.com/j4n0/jobsket/tree/master/sources/main/ui/custom/segControl



回答2:

I use buttons with images that act like radio buttons. i.e. an off image and an on image. The nice thing about this approach is it is very simple to implement and you can use the image to control the state of the button control. It works as a simple toggle and no fancy button state is needed. You can easily add code to the method that does something when buttons are on or off, in the example I just call a method which is writing some user defaults.

It should be quite easy to adapt to create radio button functionality.

 -(IBAction)tickboxControl:(id)sender{  
      NSLog(@"%s",__FUNCTION__);  
      bgImageOn = [UIImage imageNamed:@"tickedBox.png"];  
      bgImageOff = [UIImage imageNamed:@"tickBoxEmpty.png"];  
      UIButton *buttonClicked = (UIButton *)sender;  
   UIImage *imageOfClicked = [buttonClicked imageForState:UIControlStateNormal];  
      if (imageOfClicked == bgImageOff) {  
           [self setButtonFlags: [NSNumber numberWithInt:[sender tag]] : [NSNumber numberWithInt:1] ];  
           [buttonClicked setImage:bgImageOn forState:UIControlStateNormal];  
      } else{  
           [self setButtonFlags: [NSNumber numberWithInt:[sender tag]] : [NSNumber numberWithInt:0] ];  
           [buttonClicked setImage:bgImageOff forState:UIControlStateNormal];  
      }       
 }


回答3:

I created a project that shows exactly how to achieve what you are asking to do. You can change the graphics from checkboxes (which I find a little better to see and understand on a device) to radio buttons.

Read the debug log to understand the logic.

If you have any questions give me a shout.

Radio Buttons Example Project (XCode4)

[this is in reply to your comments below]



回答4:

I’ve used a UISegmentedControl as radio buttons.



回答5:

Try this for a radio button in iOS:

@interface RadioButtonViewController : UIViewController

{
    //for radio button
    IBOutlet UIButton *radioButton1;
    IBOutlet UIButton *radioButton2;
    IBOutlet UITextField *selectedValue;
    IBOutlet UILabel *label1;
    IBOutlet UILabel *label2;
   }

@property (nonatomic,retain) IBOutlet UIButton *radioButton1;
@property (nonatomic,retain) IBOutlet UIButton *radioButton2;
@property (nonatomic,retain) IBOutlet UITextField *selectedValue;
-(IBAction)userChangedButtonClicked:(id)sender
@end

Write code in .m file and specify the default and selected image in storyboard inspector window and give the tag value each button.

RadioButtonViewController.m

-(IBAction)userChangedButtonClicked:(id)sender
{
    UIButton *senderBtn = (UIButton*)sender;
    if (senderBtn.tag == 101 && !self.radioButton1.selected)
    {
       self.radioButton1.selected = TRUE;
        self.radioButton2.selected = FALSE;
      selectedValue.text = label1.text;
    }else if (senderBtn.tag == 102 && !self.radioButton2.selected)
    {
        self.radioButton1.selected = FALSE;
        self.radioButton2.selected = TRUE;
       selectedValue.text = label2.text;
    }
}