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