0

我有一个视图,其中包含用户动态添加的标签。如果用户想编辑任何标签,他们按下一个按钮,所有标签都会通过删除按钮和移动按钮突出显示。(编辑是我稍后会跨越的另一座桥)。

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

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

这是我有的几种方法:

- (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];
         }
    }
}
4

3 回答 3

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];
         }
    }
}
于 2013-01-23T04:49:50.940 回答
0

尝试这个

UIView *   seletetButton = nil;
for (UIView * btn in self.view.subviews){
    if ([btn isKindOfClass:[UIButton class]])
    {
        if (seletetButton.tag != btn.tag)
        {
            [btn removeFromSuperview];
        }
    }
}
于 2013-01-23T06:40:15.163 回答
0

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

于 2013-01-23T03:34:19.097 回答