UITextView
中有UIViewController
。在UITextView
中,需要为笔记插入多个复选框。
如何创建多个checkBox?
I have created Multiple CheckBoxes for `UIButton` Click, But When I Select or DeSelect operation, all ChecKBoxes Value changes.
如何动态创建多个复选框并为这些CheckBox创建方法?
有可能吗?
这是我的代码:
-(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
答案 0 :(得分:6)
选择NSMutableArray
..
NSMutableArray *selectedBtnarr;
.m文件中的
- (void)viewDidLoad
{
selectedBtnarr=[NSMutableArray alloc]init];
}
然后,您必须设置UIButton的tag属性。每个Button都有不同的标签。
-(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];
}
}
答案 1 :(得分:1)
尝试使用任何整数设置要创建的按钮的标记(使用循环)。然后使用-(void)ChkUnChk:(id)sender{}
调用选择器。因此,仅为标记按钮调用该方法。
答案 2 :(得分:1)
答案 3 :(得分:0)
记下以下两种方法。由于您已经为不同的状态分配了按钮的不同方法,因此您只需要更改这些状态并按照按钮的状态执行代码。
-(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];
}
}
如果你卡在某个地方,请告诉我。
感谢。