Raj*_*ran 1 iphone checkbox objective-c uitextview ios
我有一个UITextView在UIViewController.其中UITextView,需要为备注插入多个CheckBox.
如何创建多个checkBox?
I have created Multiple CheckBoxes for `UIButton` Click, But When I Select or DeSelect operation, all ChecKBoxes Value changes.
Run Code Online (Sandbox Code Playgroud)
如何动态创建多个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];
}
Run Code Online (Sandbox Code Playgroud)
这里注意 - > UITextView,复选框 - >UIButton
拿一个NSMutableArray......
在.h文件中
NSMutableArray *selectedBtnarr;
Run Code Online (Sandbox Code Playgroud)
在.m文件中
- (void)viewDidLoad
{
selectedBtnarr=[NSMutableArray alloc]init];
}
Run Code Online (Sandbox Code Playgroud)
然后,您必须设置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];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5227 次 |
| 最近记录: |