开发者

Radio button and saving the state values in iphone

I need to create two r开发者_如何学Goadio 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 :

Radio button and saving the state values in iphone

Radio button and saving the state values in iphone

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜