我在主视图中添加了一个子视图,但是我无法从该视图中将其删除。

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。我想展示它,然后在我按下按钮时将其删除,仅此而已。

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

谢谢

最佳答案

您正在创建其他引用,然后尝试将其从其 super 视图中删除,而根本不添加它。我将尝试解释:
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];
}

10-07 23:11