我在UIViewController
中有UITextView
。在UITextView
中,需要爲筆記插入多個複選框。如何以編程方式創建多個CheckBox
如何創建多個複選框?
I have created Multiple CheckBoxes for `UIButton` Click, But When I Select or DeSelect operation, all ChecKBoxes Value changes.
如何動態創建多個CheckBox併爲這些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
太感謝你了。它的工作很好。我怎樣才能刪除這些複選框使用退格鍵或刪除鍵 – Rajendran