iOS中单选按钮的实现

iOS中单选按钮的实现,ios,objective-c,Ios,Objective C,我试图在我的项目中实现单选按钮。由于现成的控件不适用于它,所以我创建了简单的按钮并设置它们的图像,当它们被单击时图像会发生变化。现在我希望,当我单击它们时,应该为字符串指定适当的值,这些值反过来会更新到sqlite数据库中。代码如下: 婚姻状况单选按钮的方法: -(IBAction)maritalStatusRadioButton:(id)sender { if(mMaritalRadioButton.isHighlighted) { [self radio

我试图在我的项目中实现单选按钮。由于现成的控件不适用于它,所以我创建了简单的按钮并设置它们的图像,当它们被单击时图像会发生变化。现在我希望,当我单击它们时,应该为字符串指定适当的值,这些值反过来会更新到sqlite数据库中。代码如下:

婚姻状况单选按钮的方法:

-(IBAction)maritalStatusRadioButton:(id)sender
{


    if(mMaritalRadioButton.isHighlighted)
    {

        [self radioButtonClick:sender];
    }
}
男性/女性单选按钮的方法:

-(IBAction)genderMaleRadioButton:(id)sender
{
    if(mMaleRadioButton.isHighlighted){
        mFemaleRadioButton.enabled = NO;

       // [sender setImage: [UIImage imageNamed:@"Radio_button_on.png"]forState:UIControlStateNormal];
        [self radioButtonClick:sender];
    }else if (mFemaleRadioButton.isHighlighted){
        mMaleRadioButton.enabled = NO;
        [self radioButtonClick:sender];

    }
RadioButton单击方法:

-(void) radioButtonClick:(id)sender
{
     [sender setImage: [UIImage imageNamed:@"Radio_button_on.png"]forState:UIControlStateNormal];
}
更新方法:

-(IBAction)saveUserProfile
{
    sqlite3_stmt *statement;
    const char *dbpath = [mDatabasePath UTF8String];

    if (sqlite3_open(dbpath, &mDiary) == SQLITE_OK)
    {

        NSString *marital = [[NSString alloc]init];  //holds marital status of user
        NSString  *gender = [[NSString alloc]init];  //used to determine if user has entered male or female gender

        if (mMaritalRadioButton.isHighlighted) {
            marital = [NSString stringWithFormat:@"Married"];
        }
        else{
            marital  = [NSString stringWithFormat:@"Unmarried"];
        } 
        if(mMaleRadioButton.isHighlighted)
        {
            gender = [NSString stringWithFormat:@"Male"];
        }else{
            gender = [NSString stringWithFormat:@"Female"];
        }
        NSString *dummy = [[NSString alloc] initWithFormat:@"%@",self.getUserName];
        //NSLog(@"dummy string is %@",dummy);

        NSString *updateSQL = [NSString stringWithFormat:
                               @"UPDATE USERDETAIL SET mUname = \"%@\", mImage = \"%@\", mGender = \"%@\", mDob = \"%@\", mEmail = \"%@\", mAddress = \"%@\", mLatitude = \"%@\", mLongitude = \"%@\",mMaritalStatus = \"%@\" WHERE mUserName = \"%@\" ", mUname.text, @"", gender, @"", mEmail.text, mAddress.text, mLatitude.text,mLongitude.text,marital, dummy];
               const char *update_stmt = [updateSQL UTF8String];
        sqlite3_prepare_v2(mDiary, update_stmt,
                           -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
       {
            mStatus.text = @"user profile updated";
            mUname.text = @"";
           // mPassword.text = @"";
            //mImage.text = @"";
            mGender.text = @"";
            //mDob.date = @"";
            mEmail.text = @"";
            mAddress.text = @"";
            //mMaritalStatus.text = @"";
            mLatitude.text = @"";
            mLongitude.text = @"";


        } else {
            mStatus.text = @"Failed to update user profile";
        }

        sqlite3_finalize(statement);
        sqlite3_close(mDiary);
    }

}
问题在于,在saveUserProfile方法中,条件mMaritalRadioButton.isHighlighted和mMaleRadioButton.isHighlighted永远不会为真。因此,只有女性和未婚者才会被储存。男性和已婚者从不储存。图像更改没有问题。有人能帮忙解决吗?
提前感谢。

isHighlighted用于检查临时状态,当您点击按钮时,它将高亮显示,表示按钮上将显示蓝色。这是暂时的,所以你不能依赖它

而不是:

if(mMaritalRadioButton.isHighlighted)
使用:

因为isHiglighted是错误的财产。 重要的部分是什么时候画的

你需要选举
请注意,您必须在iAction中手动设置它。

@Daij Djan是正确的,您需要的是isSelected,而不是isHighlighted

当用户按下按钮但尚未释放触摸时,使用isHighlighted

此外,您不需要每次都像这样更改图像,而是为选中的uicontrol状态添加图像,如下所示:

[button setImage: [UIImage imageNamed:@"Radio_button_on.png"]
        forState: UIControlStateSelected];

为什么要比较图像?所选属性是为此创建的,否@Daij Djan:如果您需要使用所选属性,您需要在单击它时将所选属性设置为“是”或“否”。您不需要创建另一个UIImage实例来进行比较。非常感谢。。。现在一切都很好。你能告诉我在我的代码中到底是什么问题吗?@Crazed'n'dazd我们都做了^^ isHighlighted是错的。仅用于绘图,不保存。检查图像不是很坚固谢谢你的回复。。。但这不起作用。我更改了radioClickButton:[sender setImage:[UIImage ImageName:@Radio_button_on.png]中的状态:UIControlStateSelected];不变。。现在图像也没有改变。forControlState=>forState
Discussion
Returns a Boolean value that indicates whether the control is highlighted when it is drawn. 
Discussion
Specify YES if the control is selected; otherwise NO. The default is NO. For many controls, this state has no effect on behavior or appearance. But other subclasses (for example, UISwitchControl) or the application object might read or set this control state.
[button setImage: [UIImage imageNamed:@"Radio_button_on.png"]
        forState: UIControlStateSelected];
//Radio Button : Tap on this each time will toggle its state.


//// Initialization...

UIButton *radioButton = [[UIButton alloc] initWithFrame:CGRectMake(10,10,100,100)];

[radioButton setImage:[UIImage imageNamed:@"unSelected.png"] forState:UIControlStateNormal];

[radioButton setImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];


////// target method 

-(void)on_click_button:(id)sender
{
  UIButton *button = (UIButton*)sender;

  [button setSelected:!button.isSelected];

  if(button.isSelected)
  {
     //Write your code on selected state here...
  }
  else
     {
        //Write your code on un-selected state here...
     }

}