在我的应用程序中,在 UIButton Click 上,我需要动态创建多个 UIButton 并为此执行方法。
如何从创建的 UIButtons 中删除特定的 UIButton?按退格键或删除键?或者有没有其他方法可以删除 UIButton?
我的创建源代码
在 .h 文件中
NSMutableArray *selectedBtnarr;
在 .m 文件中
-(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 setTag:checkButtonCount];
[checkbox addTarget:self action:@selector(ChkUnChk:) forControlEvents:UIControlEventTouchUpInside];
[note addSubview:checkbox];
NSString *xAxis=[NSString stringWithFormat:@"%f" ,checkbox.frame.origin.x];
NSString *yAxis=[NSString stringWithFormat:@"%f" ,checkbox.frame.origin.y];
[chkBoxX addObject:xAxis];
[chkBoxY addObject:yAxis];
NSString *tag=[NSString stringWithFormat:@"%d" ,checkButtonCount];
[chkTag addObject:tag];
checkButtonCount=checkButtonCount+1;
}
-(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];
}
}
但我不知道如何删除这个......
删除我的代码如下
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
const char * _char = [text cStringUsingEncoding:NSUTF8StringEncoding];
int isBackSpace = strcmp(_char, "\b");
if (isBackSpace == -8)
{
NSLog(@"backspace Detected");
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;
NSLog(@"Cursor Place X : %f Y: %f",cursor.x,cursor.y);
int tagcoount=[chkTag count];
for(int a=0;a<tagcoount;a++)
{
NSString *tag=[NSString stringWithFormat:@"%@" ,[chkTag objectAtIndex:a]];
int chkcnt=[tag intValue];
if(cursor.x==checkbox.frame.origin.x && cursor.y==checkbox.frame.origin.y && a==checkbox.tag)
{
[[checkbox viewWithTag:chkcnt] removeFromSuperview];
[chkTag removeObjectAtIndex:chkcnt];
}
}
}
但它只删除最后一个 UIButton ,当光标仅在当前位置时也是如此......给我正确的想法