使用数组动态删除按钮

时间:2013-01-23 03:29:20

标签: objective-c uibutton labels

我的视图中有标签由用户动态添加。如果用户想要编辑任何标签,他们按下按钮,所有标签都会被删除按钮和移动按钮突出显示。 (编辑是我稍后会遇到的另一个桥梁。)

我的问题是:打开和关闭按钮的最佳方法是什么?我有一个方法可以打开按钮...但是在完成编辑时如何关闭它们我感到很遗憾。我是否需要标记我的按钮然后“隐藏它们”?或者我只是完全删除它们?如何解析所有打开的按钮,然后关闭它们。我是否还需要将它们放入数组中?标签上标有唯一的数字,所以我知道哪个标签是哪个。

有什么想法?指导?如果我做错了请告诉我。

我有几种方法:

- (void) showEditableText {
// Parse the array of labels
    if(textArray.count > 0){
        for(UILabel *label in textArray){

//Add Delete Button           
 UIImage * delButtonImage = [UIImage imageNamed:@"GUI_Delete.png"];
            UIButton * delThisButton = [[UIButton alloc] initWithFrame:CGRectMake(label.frame.origin.x - delButtonImage.size.width, label.frame.origin.y - delButtonImage.size.height, delButtonImage.size.width, delButtonImage.size.height)];
 [delThisButton setBackgroundImage:delButtonImage forState:UIControlStateNormal];
            [delThisButton addTarget:self action:@selector(deleteThisLabel:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:delThisButton];

            //Add a move button
            UIImage * moveButtonImage = [UIImage imageNamed:@"GUI_Move.png"];
            UIButton * moveThisButton = [[UIButton alloc] initWithFrame:CGRectMake((label.frame.origin.x + label.frame.size.width + moveButtonImage.size.width), label.frame.origin.y - moveButtonImage.size.height, moveButtonImage.size.width, moveButtonImage.size.height)];
 [moveThisButton setBackgroundImage:moveButtonImage forState:UIControlStateNormal];
            [moveThisButton addTarget:self action:@selector(moveThisLabel:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:moveThisButton];

//Make the text highlighed
            label.highlighted = YES;
            label.backgroundColor  = [UIColor colorWithRed:203/255.0f green:230/255.0f blue:239/255.0f alpha:1];
            label.highlightedTextColor = [UIColor redColor];
        }
    }
}


- (void) doneEditingText {
    if(textArray.count > 0){
        for(UILabel *label in textArray){
//THIS IS WHERE I AM STUCK? WHAT DO I DO?

            label.highlighted = NO;
            label.backgroundColor  = [UIColor clearColor];
         }
    }
}

3 个答案:

答案 0 :(得分:2)

//inside your first method set the same tag to your all buttons

-(void) showEditableText {

........
.......
.......
 delThisButton.tag = 10;

 moveThisButton.tag = 10;

}

//inside your second method delete all the subviews using this tag as shown below..

-(void) doneEditingText {

    if(textArray.count > 0){
        for(UILabel *label in textArray){
..............................

//THIS IS WHERE I AM STUCK? WHAT DO I DO?

    for (UIView *subview in [self.view subviews]) {
        if (subview.tag == 10) {
            [subview removeFromSuperview];
        }
    }
...............................

            label.highlighted = NO;
            label.backgroundColor  = [UIColor clearColor];
         }
    }
}

答案 1 :(得分:0)

将所有按钮代码移至viewDidLoad,隐藏它们(button.hidden = YES)。当您开始/完成文本编辑时,取消隐藏并隐藏按钮。你需要有一个ivar来包含按钮。所以将它们添加到.h文件中。

答案 2 :(得分:0)

试试这个

UIView *   seletetButton = nil;
for (UIView * btn in self.view.subviews){
    if ([btn isKindOfClass:[UIButton class]])
    {
        if (seletetButton.tag != btn.tag)
        {
            [btn removeFromSuperview];
        }
    }
}