在iOS中实现单选按钮

时间:2013-05-02 09:27:48

标签: ios objective-c

我正在尝试在我的项目中实现单选按钮。由于现成的控件不可用,因此我创建了简单的按钮并设置了它们的图像,这些图像在点击时会发生变化。现在我希望当我点击它们时,应该为字符串分配适当的值,然后将其更新到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永远不会成立。因此,只有女性和未婚者被存储。从未存过男性和已婚人士。图像更改没有问题。任何人都可以帮助解决它吗?                  提前谢谢。

4 个答案:

答案 0 :(得分: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...
     }

}

答案 1 :(得分: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中手动设置

答案 2 :(得分:0)

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

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

isHighlighted

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

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

答案 3 :(得分:-7)

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

而不是:

if(mMaritalRadioButton.isHighlighted)

使用:

if([[UIImage imageNamed:@"Radio_button_on.png"] isEqual:mMaleRadioButton.currentImage])