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

How to delete multiple UIButton or particular UIButton dynamically created?

在我的应用程序中,单击一个uibutton,我需要动态创建多个uibuttons并为此执行方法。

如何从创建的uibuttons中删除特定uibutton?按退格键还是按删除键?还是有其他方法删除ui按钮?

用于创建的源代码

在.h文件中

1
NSMutableArray *selectedBtnarr;

在m文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
-(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];
    }
    }

但我不知道怎么删除这个…

删除我的代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
- (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];

                }
        }
}

但它只删除最后一个ui按钮,当光标仅位于当前位置时也是如此。给我个好主意


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


根据您的要求:请从我的Mac项目(不是iOS)中获得这个想法,但您会觉得这很方便。

1
2
3
4
5
6
7
8
9
10
11
@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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
@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