-3

在我的应用程序中,在 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 ,当光标仅在当前位置时也是如此......给我正确的想法

4

2 回答 2

2

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

于 2012-12-18T16:31:31.280 回答
1

根据您的要求:请从我的 Mac项目(不在 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
于 2012-12-18T16:46:06.393 回答