uibutton只显示在新添加的uiview中

时间:2012-01-06 10:32:18

标签: ios uiview uibutton

我坚持要显示uibutton * removeSticker,请帮助。

- (void)viewWillAppear:(BOOL)animated {

UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, _imagePicker.selectedImage.size.width, _imagePicker.selectedImage.size.height)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageView setImage:_imagePicker.selectedImage];
[holderView addSubview:imageView];

removeSticker = [UIButton buttonWithType:UIButtonTypeCustom];
removeSticker.frame = CGRectMake(0, 0, 32, 32);
[removeSticker setImage:[UIImage imageNamed:@"cancel-disabled.png"] forState:UIControlStateNormal];
[removeSticker addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
[holderView addSubview: removeSticker];
[removeSticker setHidden: YES];

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];

UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];

UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[longPressRecognizer setDelegate:self];
[holderView addGestureRecognizer:longPressRecognizer];

[stickerView addSubview:holderView];

}

- (void)buttonClicked:(id)sender{
UIView *holderView = [(UIButton *)sender superview]; [holderView removeFromSuperview];
}

uibutton * removeSticker仅出现在最新添加的uiview * holderView上,如果我想编辑之前添加的一次,则不会发生任何事情。请帮助,非常感谢。

-(void)longPress:(id)sender {
[removeSticker setHidden:NO];
}

请查看随附的屏幕截图:http://i.stack.imgur.com/moMOj.png

对于上面的截图,瓶子和西瓜是*holderView。 我先加了瓶子,然后加了西瓜,但是当我点击瓶子时,[removeSticker setHidden:NO];出现在西瓜而不是瓶子里。

[removeSticker setHidden:NO];仅显示最新添加的*holderView

1 个答案:

答案 0 :(得分:0)

来自Apple's documentation

“视图可以同时只有一个超级视图。”

因此,如果您将视图作为子视图添加到另一个视图,则子视图将从其先前的超级视图中删除并添加到新视图中。 (请参阅addSubview方法的文档)。

(考虑为什么UIView有一个类型为UIView * superview的@property,而不是一个名为superviews [复数]的NSArray)。

修改:如果您确实希望在多个超级视图中拥有多个视图,则必须创建UIView的多个实例,并将多个实例添加到各种超级视图中。您还需要处理相应的UIView实例,因此您可以将它们添加到NSArray并使用NSDictionary管理该aeray。