Ios 尝试删除ScrollViewController中的子视图

Ios 尝试删除ScrollViewController中的子视图,ios,uiview,uiscrollview,Ios,Uiview,Uiscrollview,我想在点击按钮时删除对象,但它仅适用于第一个视图,其他视图不删除 请帮帮我 这是我的密码 self.frame = CGRectMake(0, 0, 1024, 768); UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; deleteButton.frame = CGRectMake(0, 0, 100, 100); [deleteButton setTi

我想在点击按钮时删除对象,但它仅适用于第一个视图,其他视图不删除

请帮帮我

这是我的密码

    self.frame = CGRectMake(0, 0, 1024, 768);



    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    deleteButton.frame = CGRectMake(0, 0, 100, 100);
    [deleteButton setTitle:@"fuck" forState:UIControlStateNormal];
    [deleteButton addTarget:self action:@selector(removeViews) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:deleteButton];




    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(100, 200, 400, 100)];
    [scrollView setScrollEnabled:YES];
    scrollView.pagingEnabled = YES;
    [scrollView setDelegate:self];
    scrollView.backgroundColor = [UIColor brownColor];
    [scrollView setShowsHorizontalScrollIndicator:YES];
    [scrollView setIndicatorStyle:UIScrollViewIndicatorStyleDefault]; scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    scrollView.contentMode = UIViewContentModeScaleToFill;
    [scrollView setContentSize:CGSizeMake(1000,100)];

    [self addSubview:scrollView];


    [self setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.8]];


    circleView = [[UIView alloc] initWithFrame:CGRectMake(100,5,90,90)];
    circleView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];
    //circleView.layer.borderColor = [[UIColor blackColor] CGColor];
    //circleView.layer.borderWidth = 4;

    UILabel* circleIndex = [[UILabel alloc] init];
    circleIndex.frame    = CGRectMake(30, 25, 40, 40);
    [circleIndex setFont:[UIFont fontWithName:@"Arial-BoldMT" size:40]];
    [circleIndex setText:@"A"];

    [circleView addSubview:circleIndex];


    circleView2 = [[UIView alloc] initWithFrame:CGRectMake(100+100,5,90,90)];
    circleView2.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];

    circleView3 = [[UIView alloc] initWithFrame:CGRectMake(100+100+100,5,90,90)];
    circleView3.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];

    circleView4 = [[UIView alloc] initWithFrame:CGRectMake(100+100+100+100,5,90,90)];
    circleView4.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];

    circleView5 = [[UIView alloc] initWithFrame:CGRectMake(100+100+100+100+100,5,90,90)];
    circleView5.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];

    circleView6 = [[UIView alloc] initWithFrame:CGRectMake(100+100+100+100+100+100,5,90,90)];
    circleView6.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];

    [scrollView addSubview:circleView];

    [scrollView addSubview:circleView2];

    [scrollView addSubview:circleView3];

    [scrollView addSubview:circleView4];

    [scrollView addSubview:circleView5];

    [scrollView addSubview:circleView6];



        exitView = [[UIView alloc] initWithFrame:CGRectMake(70,-5,30,30)];
        exitView.layer.cornerRadius = 15;
        exitView.backgroundColor = [UIColor redColor];
        exitView.layer.borderColor = [[UIColor whiteColor] CGColor];
        exitView.layer.borderWidth = 2;


        [circleView addSubview:exitView];

            UIView *exitLabel = [[UIView alloc] initWithFrame:CGRectMake(8,13,15,3)];
            exitLabel.backgroundColor = [UIColor clearColor];
            exitLabel.layer.borderColor = [[UIColor whiteColor] CGColor];
            exitLabel.layer.borderWidth = 2;

            [exitView addSubview:exitLabel];


    UILongPressGestureRecognizer *longPress =
    [[UILongPressGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(handleLongPress:)];
    [circleView addGestureRecognizer:longPress];




    UITapGestureRecognizer *singlePress =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(handleSinglePress:)];
    [exitView addGestureRecognizer:singlePress];


    //[longPress release];


    UITapGestureRecognizer *singlePress2 =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(singlePress:)];
    [circleView2 addGestureRecognizer:singlePress2];



    [exitView setHidden:YES];


    //[circleView bringSubviewToFront:exitView];




    //[exitView setUserInteractionEnabled:YES];


}
return self;
}


-(void)removeViews{

[self.circleView3 removeFromSuperview];

[scrollView setNeedsDisplay];

[self setNeedsDisplay];
}

我可以删除唯一的circleView,而不能删除其他circleView(circleView2、circleView3等)

尝试这样做:使用标签和按钮将每个circleView添加到滚动视图中,以删除此特定视图

self.frame = CGRectMake(0, 0, 1024, 768);

scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(100, 200, 400, 100)];
[scrollView setScrollEnabled:YES];
scrollView.pagingEnabled = YES;
[scrollView setDelegate:self];
scrollView.backgroundColor = [UIColor brownColor];
[scrollView setShowsHorizontalScrollIndicator:YES];
[scrollView setIndicatorStyle:UIScrollViewIndicatorStyleDefault]; scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.contentMode = UIViewContentModeScaleToFill;
[scrollView setContentSize:CGSizeMake(1000,100)];

//number of circleviews you want to add to your scrollview
int countViews = 6;

//for each number add one circleview to your scrollview. for each circleview add a button, to remove the view from the scrollview
for(int i=0;i<countViews;i++){

    UILabel* circleIndex = [[UILabel alloc] init];
    circleIndex.frame    = CGRectMake(30, 25, 40, 40);
    [circleIndex setFont:[UIFont fontWithName:@"Arial-BoldMT" size:40]];
    [circleIndex setText:@"A"];

    [circleView addSubview:circleIndex];

    UIView *circleView = [[UIView alloc] initWithFrame:CGRectMake(100+(i*100),5,90,90)];
    circleView.backgroundColor = [UIColor whiteColor];
    circleView.tag = i;
    [scrollView addSubview:circleView];

    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteButton setBackgroundColor:[UIColor clearColor]];
    deleteButton.frame = circleView.frame;
    deleteButton.tag = 100+i;
    [deleteButton addTarget:self action:@selector(removeViews:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:deleteButton];
}

[self addSubview:scrollView];

[self setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.8]];

exitView = [[UIView alloc] initWithFrame:CGRectMake(70,-5,30,30)];
exitView.layer.cornerRadius = 15;
exitView.backgroundColor = [UIColor redColor];
exitView.layer.borderColor = [[UIColor whiteColor] CGColor];
exitView.layer.borderWidth = 2;


[circleView addSubview:exitView];

UIView *exitLabel = [[UIView alloc] initWithFrame:CGRectMake(8,13,15,3)];
exitLabel.backgroundColor = [UIColor clearColor];
exitLabel.layer.borderColor = [[UIColor whiteColor] CGColor];
exitLabel.layer.borderWidth = 2;

[exitView addSubview:exitLabel];


UILongPressGestureRecognizer *longPress =
[[UILongPressGestureRecognizer alloc] initWithTarget:self
                                              action:@selector(handleLongPress:)];
[circleView addGestureRecognizer:longPress];




UITapGestureRecognizer *singlePress =
[[UITapGestureRecognizer alloc] initWithTarget:self
                                        action:@selector(handleSinglePress:)];
[exitView addGestureRecognizer:singlePress];


//[longPress release];


UITapGestureRecognizer *singlePress2 =
[[UITapGestureRecognizer alloc] initWithTarget:self
                                        action:@selector(singlePress:)];
[circleView2 addGestureRecognizer:singlePress2];



[exitView setHidden:YES];

}

- (IBAction)removeViews:(id)sender{
    [[scrollView viewWithTag:[sender tag]] removeFromSuperview];
    [[scrollView viewWithTag:([sender tag]-100)] removeFromSuperview];
}
self.frame=CGRectMake(0,0,1024768);
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(100200400100)];
[滚动视图设置可滚动:是];
scrollView.PaginEnabled=是;
[scrollView setDelegate:self];
scrollView.backgroundColor=[UIColor brownColor];
[滚动视图设置ShowShorizontalsCrollinIndicator:是];
[scrollView setIndicatorStyle:UIScrollViewIndicatorStyleDefault];scrollView.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoResizingFlexibleHight;
scrollView.contentMode=UIViewContentModeScaleToFill;
[scrollView setContentSize:CGSizeMake(1000100)];
//要添加到滚动视图的圈视图数
int countview=6;
//对于每个数字,在滚动视图中添加一个圈视图。为每个circleview添加一个按钮,以从scrollview中删除视图

对于(int i=0;i尝试为CircleView提供标记,并具有从scrollView中迭代子视图并相应删除它们的逻辑。不只是them@Bhargavi首先我将子视图添加到scrollview,然后将scrollview添加到主视图,但仍然没有change@erdemgc请试着按照Anupdas的建议来实现tag我想出来了,synthezitai这是必要的。