从UIViewController中删除子视图

时间:2014-05-20 15:11:03

标签: ios objective-c uiviewcontroller addsubview

我在主视图中添加了子视图,但之后我无法将其从此视图中删除。

NextViewController *nextView = [[NextViewController alloc] init];

nextView.transitioningDelegate = self;
nextView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.presentedViewController.view addSubview:nextView.view];
nextView.view.frame = CGRectMake(724, 80, 300, 150);

UIButton *stayButton = [[UIButton alloc] initWithFrame:CGRectMake(101, 94, 90, 49)];
[stayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[stayButton setTitle:@"Detener" forState:UIControlStateNormal];
[stayButton addTarget:self
           action:@selector(waitForNextView)
 forControlEvents:UIControlEventTouchUpInside];
[nextView.view addSubview:stayButton];
stayButton.titleLabel.font = [UIFont systemFontOfSize:25];
[stayButton setTitleColor:[UIColor colorWithRed:(255/255.0) green:(255/255.0) blue:(255/255.0) alpha:1] forState:UIControlStateNormal];

这里我在视图中添加了一个按钮,然后当按下按钮时,它会变为` -

(void)waitForNextView{
    transition = NO;
    NextViewController *nextView = [[NextViewController alloc] init];
    SectionViewController *sectionView = [[SectionViewController alloc]init];
    [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
    actualSection = 0;
    [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(dismissView2) userInfo:nil repeats:NO];
}

我试过了:

[nextView.view removeFromSuperView];

[nextView.presentedViewController removeFromParentViewController];

但我不知道我还能做些什么:S

如果您需要更多信息,nextView是一个300x100大小的UIViewController。我想要呈现它,然后当我按下按钮时将其删除,这就是全部。

如果有人可以帮助我,我会很棒。

由于

3 个答案:

答案 0 :(得分:2)

您正在创建不同的引用,然后您尝试将其从超级视图中删除而根本不添加。我会试着解释一下: 1.当您将nextView.view添加到self.view时,您已创建了此类实例

NextViewController *nextView = [[NextViewController alloc] init];

2。当您尝试删除它时,而不是引用上面的实例,而不是再次使用waitForNextView方法创建一个全新的实例

 NextViewController *nextView = [[NextViewController alloc] init];

然后您尝试将其删除。请注意,nextView的最后一个实例尚未添加到任何位置,这就是您的代码无法正常工作的原因。

为了工作,您应该参考您添加到self.view的第一个参考。请调整您的代码,如下所示

ViewController.m

@interface ViewController()
{
 NextViewController *nextView;
}

添加viewController

nextView = [[NextViewController alloc] init];

nextView.transitioningDelegate = self;
nextView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.presentedViewController.view addSubview:nextView.view];
nextView.view.frame = CGRectMake(724, 80, 300, 150);

UIButton *stayButton = [[UIButton alloc] initWithFrame:CGRectMake(101, 94, 90, 49)];
[stayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[stayButton setTitle:@"Detener" forState:UIControlStateNormal];
[stayButton addTarget:self
           action:@selector(waitForNextView)
 forControlEvents:UIControlEventTouchUpInside];
[nextView.view addSubview:stayButton];
stayButton.titleLabel.font = [UIFont systemFontOfSize:25];
[stayButton setTitleColor:[UIColor colorWithRed:(255/255.0) green:(255/255.0) blue:(255/255.0) alpha:1] forState:UIControlStateNormal];

最后在你的IBAction方法

-(void)waitForNextView{
    transition = NO;
    [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
    actualSection = 0;
    [nextView removeFromSuperview];
}

答案 1 :(得分:0)

这里的问题是您将按钮作为子视图添加到nextView的视图中,因此删除nextView的视图将无法正常工作。

如果您为按钮添加标签,则可以使用标签检索子视图。例如。定义按钮时:

[stayButton setTag:1];

然后在waitForNextView方法中,您可以获得如下控件:

UIButton *stayButton = (UIButton*)[nextView.view viewWithTag:1];
[stayButton removeFromSuperView];

答案 2 :(得分:0)

在方法中使用此代码解除View 2

for (UIView *subview in view.subviews ){
    [subview removeFromSuperview];
}