如何删除多个 UIButton 或动态创建的特定 UIButton?

Posted

技术标签:

【中文标题】如何删除多个 UIButton 或动态创建的特定 UIButton?【英文标题】:How to delete multiple UIButton or particular UIButton dynamically created? 【发布时间】:2012-12-18 16:27:20 【问题描述】:

在我的应用程序中,在单击 UIButton 时,我需要动态创建多个 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 ,当光标仅在当前位置时也是如此......给我正确的想法

【问题讨论】:

你检查过我的答案并尝试下载我的代码吗? 你用委托写代码..所以我没有下载你的代码 该代码在 AppDelegate 类中,您可以轻松将该代码移动到您的类文件中...有什么大不了的? 【参考方案1】:

使用NSMutableArray 存储UIButton 对象。使用setTag: 方法为每个UIButton 分配一个标签号,以识别各个按钮并相应地删除它们。

【讨论】:

非常感谢..我已经更新了代码。请检查它并说出如何写删除 您是否尝试过使用 NSLog 来查看发送到 [chkTag removeObjectAtIndex:chkcnt] 的 chkcnt 值?【参考方案2】:

根据您的要求:请从我的 Mac project 获得这个想法(因为我不在 ios 中),但您会觉得这很方便。

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

@property(strong)NSMutableArray *buttons;
- (IBAction)add:(id)sender;
- (IBAction)remove:(id)sender;
@property (strong) IBOutlet NSPopUpButton *popUp;
@property (strong) IBOutlet NSView *buttonView;

@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    _buttons=[NSMutableArray new];



-(void)clearButtonSubview
    for (NSInteger i=0; i<[[_buttonView subviews]count]; i++) 
        NSView *view=[[_buttonView subviews]objectAtIndex:i];
        [view removeFromSuperview];
    


-(void)drawButtons

    //when u add, remove prev drawn and redraw all.
    [self clearButtonSubview];

    for (NSInteger i=0; i<[_buttons count]; i++) 
        NSButton *button=[_buttons objectAtIndex:i];
        [button setFrame:NSMakeRect(i*55, 10, 50, 20)];
        [_buttonView addSubview:button];

        //add buttons in popup
        [_popUp addItemWithTitle:[NSString stringWithFormat:@"%ld",button.tag]];
    



- (IBAction)add:(id)sender 

    [_popUp removeAllItems];

    NSButton *bttn=[NSButton new];
    //get last button tag
    NSInteger lastButtonTag=[[_buttons lastObject] tag];

    [bttn setTag:lastButtonTag+1];
    [bttn setTitle:[NSString stringWithFormat:@"%ld",[bttn tag]]];

    [_buttons addObject:bttn];

    [self drawButtons];


- (IBAction)remove:(id)sender 
    NSInteger indexOfButtonToRemove;
    NSString *selectedButton=[_popUp titleOfSelectedItem];

    for (NSInteger index=0;index<[_buttons count];index++) 
        NSButton *button=[_buttons objectAtIndex:index];

        if ([[button title]isEqualToString:selectedButton]) 
            indexOfButtonToRemove=index;
            break;
        
    

    [_buttons removeObjectAtIndex:indexOfButtonToRemove];
    [_popUp removeItemAtIndex:indexOfButtonToRemove];

    [self clearButtonSubview];
    [self drawButtons];


@end

【讨论】:

非常感谢...我已经更新了我的问题并添加了我的代码.. 请看一下并告诉我如何删除 UIButton 在方法check中,建议不要以这种方式绘制按钮,而是创建具有y的数组,然后绘制按钮。

以上是关于如何删除多个 UIButton 或动态创建的特定 UIButton?的主要内容,如果未能解决你的问题,请参考以下文章

用户单击按钮时如何动态创建和删除视图[重复]

如何删除我使用循环创建的多个按钮?

如何从谷歌地图中删除特定的多个标记而不是单个或所有标记

如何动态更改自定义 UIButton 图像

在不创建新视图或知道特定父视图的情况下向 UIButton 类添加约束

如何从 Swift 的扩展中排除特定的 UIButton?