单选按钮并在iphone中保存状态值

时间:2011-08-18 01:51:27

标签: iphone objective-c ios4

我需要创建两个单选按钮,我想在数组中存储状态值(启用或禁用)。任何人都可以共享代码吗?

2 个答案:

答案 0 :(得分:0)

这个怎么样

在头文件

中获取可变字典
NSMutableDicionary *dictionaryForRadioStates;

并在实施中

单击单选按钮

中的实现文件中的ANd
 [dictionaryForRadioStates setObject:[NSNumber numberWithBool:radioButton.enabled] forKey:@"radioButton1"];


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

你可以像这样获得价值

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

答案 1 :(得分:0)

这是使用单选按钮的完整代码:(您需要使用自定义按钮来执行此任务)

在.h档案中

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

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

.m文件

-(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;


        }
    }
}

以下是要使用的图片:

enter image description here

enter image description here

就存储而言,您可以使用Dictionary通过键值存储值。

希望这有助于编码。