gpt4 book ai didi

ios - iOS单选按钮的实现

转载 作者:行者123 更新时间:2023-11-28 17:57:35 24 4
gpt4 key购买 nike

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

}

radioButtonClick 方法:

-(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.isHighlightedmMaleRadioButton.isHighlighted 永远不会为真。因此只有 Female 和 Unmarried 被存储。 Male 和 Married 从不存储。图像更改没有问题。任何人都可以帮助解决它吗? 提前致谢。

最佳答案

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

}

关于ios - iOS单选按钮的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16334559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com