1

我有一个UITextViewUIViewController. 在那UITextView,需要为注释插入多个复选框。

如何创建多个复选框?

I have created Multiple CheckBoxes for `UIButton` Click, But When I Select or DeSelect operation, all ChecKBoxes Value changes.

如何动态创建多个复选框并为这些复选框创建方法?

是否可以?

这是我的代码:

-(void)Check
{
    CGPoint origin = note.frame.origin;
    NSString* head = [note.text substringToIndex:note.selectedRange.location];
    CGSize initialSize = [head sizeWithFont:note.font constrainedToSize:note.contentSize];
    NSUInteger startOfLine = [head length];

    NSString* tail = [head substringFromIndex:startOfLine];
    CGSize lineSize = [tail sizeWithFont:note.font forWidth:note.contentSize.width lineBreakMode:UILineBreakModeWordWrap];
    CGPoint cursor = origin;
    cursor.x += lineSize.width+15;
    cursor.y += initialSize.height - lineSize.height-130;

checkbox = [[UIButton alloc] initWithFrame:CGRectMake(cursor.x,cursor.y,15,15)];
    [checkbox setBackgroundImage:[UIImage imageNamed:@"unchk.png"]forState:UIControlStateNormal];
    [checkbox setBackgroundImage:[UIImage imageNamed:@"chk.png"]forState:UIControlStateSelected];
    [checkbox setBackgroundImage:[UIImage imageNamed:@"chk.png"]forState:UIControlStateHighlighted];
    checkbox.adjustsImageWhenHighlighted=YES;
    [checkbox addTarget:self action:@selector(ChkUnChk) forControlEvents:UIControlEventTouchUpInside];
    [note addSubview:checkbox];
}

-(void)ChkUnChk
{
    if(checkUnCheck==NO)
    {
        [checkbox setBackgroundImage:[UIImage imageNamed:@"chk.png"]forState:UIControlStateNormal];
        checkUnCheck=YES;
    }
    else if(checkUnCheck==YES)
    {
        [checkbox setBackgroundImage:[UIImage imageNamed:@"unchk.png"]forState:UIControlStateNormal];
        checkUnCheck=NO;
    }
}

-(void)checkboxSelected:(id)sender
{
    checkBoxSelected = !checkBoxSelected;
    [checkbox setSelected:checkBoxSelected];
}

这里注意 --> UITextView,复选框 -->UIButton

4

4 回答 4

7

拿一个NSMutableArray..

在 .h 文件中

NSMutableArray *selectedBtnarr;

在 .m 文件中

 - (void)viewDidLoad
   {
         selectedBtnarr=[NSMutableArray alloc]init];
   }

然后,您必须设置 UIButton 的 tag 属性。每个按钮都有不同的标签。

-(void)ChkUnChk:(id)sender
{

    UIButton *btn=(UIButton *)sender;
    NSString *Str=[NSString stringWithFormat:@"%d",btn.tag];
    BOOL flag=   [selectedBtnarr containsObject:Str];

    if (flag==YES)
    {
        [btn setBackgroundImage:[UIImage imageNamed:@"unchk.png"]    forState:UIControlStateNormal];
        [selectedBtnarr removeObject:Str];
    }
    else
    {
        [selectedBtnarr addObject:Str];
        [btn setBackgroundImage:[UIImage imageNamed:@"chk.png"] forState:UIControlStateNormal];
    }
}
于 2012-12-17T12:02:27.370 回答
1

尝试使用任何整数(使用循环)设置要创建的按钮的标签。然后使用 . 调用选择器-(void)ChkUnChk:(id)sender{}。因此,该方法将只为标记按钮调用。

于 2012-12-17T11:57:55.617 回答
1

在这里您可以找到示例源代码

https://github.com/ardalahmet/SSCheckBoxView

在此处输入图像描述

于 2012-12-17T12:07:39.650 回答
0

写下以下2种方法。由于您已经为不同的状态分配了按钮的不同方法,因此您只需更改这些状态​​并根据按钮的状态执行代码。

-(void)Check
{
    CGPoint origin = note.frame.origin;
    NSString* head = [note.text substringToIndex:note.selectedRange.location];
    CGSize initialSize = [head sizeWithFont:note.font constrainedToSize:note.contentSize];
    NSUInteger startOfLine = [head length];

    NSString* tail = [head substringFromIndex:startOfLine];
    CGSize lineSize = [tail sizeWithFont:note.font forWidth:note.contentSize.width lineBreakMode:UILineBreakModeWordWrap];
    CGPoint cursor = origin;
    cursor.x += lineSize.width+15;
    cursor.y += initialSize.height - lineSize.height-130;

    checkbox = [[UIButton alloc] initWithFrame:CGRectMake(cursor.x,cursor.y,15,15)];
    [checkbox setBackgroundImage:[UIImage imageNamed:@"unchk.png"]forState:UIControlStateNormal];
    [checkbox setBackgroundImage:[UIImage imageNamed:@"chk.png"]forState:UIControlStateSelected];
    [checkbox setBackgroundImage:[UIImage imageNamed:@"chk.png"]forState:UIControlStateHighlighted];
    checkbox.adjustsImageWhenHighlighted=YES;
    [checkbox addTarget:self action:@selector(ChkUnChk) forControlEvents:UIControlEventTouchUpInside];
    [note addSubview:checkbox];
}

-(void)ChkUnChk:(id)sender
{
    UIButton *btn = sender;
    if(btn.selected==NO)
    {
        [btn setSelected:YES];
    }
    else if(btn.selected==YES)
    {
        [btn setSelected:NO];
    }
}

如果你卡在某个地方,请告诉我。

谢谢。

于 2012-12-17T12:03:35.900 回答