简体   繁体   中英

Radio button and saving the state values in iphone

I need to create two radio buttons and I want to store there state values (enabled or disabled) in an array. can anybody share the code?

How about this

take a mutable dictionary in header file

NSMutableDicionary *dictionaryForRadioStates;

and in implementation

ANd in implementation file on tap of radio button

 [dictionaryForRadioStates setObject:[NSNumber numberWithBool:radioButton.enabled] forKey:@"radioButton1"];


[dictionaryForRadioStates setObject:[NSNumber numberWithBool:radioButton.enabled] forKey:@"radioButton2"];

You can get values back like this

BOOL state = [[dictionaryForRadioStates valueForKey:@"radioButton1"] boolValue];

This is the complete code for using the Radio Button : (You need to use the custom button to perform this task)

In .h file:

//Option Menu Buttons
IBOutlet UIButton *option1;
IBOutlet UIButton *option2;

 -(IBAction)MyCustomRadioButtons:(id)sender; // Give connection to this method through Xib/code .

.m file

-(IBAction)MyCustomRadioButtons:(id)sender
{
    if(sender==option1)
    {
        if([option1 isSelected]==TRUE)
        {
            [option1 setImage:[UIImage imageNamed:@"deselected.png"] forState:UIControlStateNormal];
            option1.selected=FALSE;

            [option2 setImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];
            option2.selected=TRUE;

        }
        else {

            [option1 setImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];
            option1.selected=TRUE;

            [option2 setImage:[UIImage imageNamed:@"deselected.png"] forState:UIControlStateNormal];
            option2.selected=FALSE;
        }

    }

    if(sender==option2)
    {
        if([option2 isSelected]==TRUE)
        {
            [option2 setImage:[UIImage imageNamed:@"deselected.png"] forState:UIControlStateNormal];
            option2.selected=FALSE;

            [option1 setImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];
            option1.selected=TRUE;

        }
        else {

            [option2 setImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];
            option2.selected=TRUE;
            [self ShowActionSheetWithPicker:sender];
            [option1 setImage:[UIImage imageNamed:@"deselected.png"] forState:UIControlStateNormal];
            option1.selected=FALSE;


        }
    }
}

Below are the images to use :

在此处输入图片说明

在此处输入图片说明

AS far as storing is concerned, you can use the Dictionary to store the values through key Values.

Hope that would help to do it with coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM