1

我正在尝试在我的项目中实现单选按钮。由于无法使用现成的控件,因此我创建了简单的按钮并设置了它们的图像,当它们被单击时会发生变化。现在我希望当我单击它们时,应将适当的值分配给字符串,然后将其更新到 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];

    }

单选按钮点击方法:

-(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条件永远不会正确。因此,只有女性和未婚者被存储。Male 和 Married 永远不会被存储。图像变化没有问题。任何人都可以帮助解决它吗?提前致谢。mMaritalRadioButton.isHighlightedmMaleRadioButton.isHighlighted

4

4 回答 4

3
//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...
     }

}
于 2013-05-02T10:36:18.037 回答
1

因为isHiglighted是错误的属性。

Discussion
Returns a Boolean value that indicates whether the control is highlighted when it is drawn. 

重要的部分是绘制时


你需要isSelected

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.

请注意,您必须在 IBAction 中手动设置它

于 2013-05-02T09:40:38.880 回答
0

@Daij-Djan 是对的,你需要isSelected,而不是isHighlighted.

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

此外,您不需要每次都像这样更改图像,而是为UIControlStateSelected这样的添加图像:

[button setImage: [UIImage imageNamed:@"Radio_button_on.png"]
        forState: UIControlStateSelected];
于 2013-05-02T09:44:52.773 回答
-7

isHighlighted 用于检查临时状态,当您点击按钮时,它将突出显示意味着按钮上会出现蓝色。它是暂时的,所以你不能依赖它。

代替:

if(mMaritalRadioButton.isHighlighted)

采用:

if([[UIImage imageNamed:@"Radio_button_on.png"] isEqual:mMaleRadioButton.currentImage])
于 2013-05-02T09:37:52.200 回答