从 UIViewController 中删除子视图
Posted
技术标签:
【中文标题】从 UIViewController 中删除子视图【英文标题】:Remove a subview from UIViewController 【发布时间】:2014-05-20 15:11:03 【问题描述】:我正在向主视图添加一个子视图,但我无法从该视图中删除它。
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。我想展示它,然后在我按下按钮时将其移除,仅此而已。
如果有人可以帮助我,我会很棒。
谢谢
【问题讨论】:
发布您尝试调用的 dissmissView2 方法 [nextView.view removeFromSuperView]; 您要删除哪个视图?是按钮还是要删除 NextView?如果 NextView 是 UIViewController 子类,要关闭它,请使用dismissViewControllerAnimated:completion:
【参考方案1】:
您正在创建不同的参考,然后您尝试将其从其超级视图中删除,而根本没有添加。我将尝试解释:
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];
【讨论】:
工作得很好,但我已经将[nextView removeFromSuperview];
更改为`[nextView.view removeFromSuperView];非常感谢Panayot,你救了我:D【参考方案2】:
这里的问题是你将按钮作为子视图添加到nextView的视图中,所以删除nextView的视图是行不通的。
如果给按钮添加标签,则可以使用标签检索子视图。例如。当你定义你的按钮时:
[stayButton setTag:1];
然后在waitForNextView
方法中,可以像这样获取控件:
UIButton *stayButton = (UIButton*)[nextView.view viewWithTag:1];
[stayButton removeFromSuperView];
【讨论】:
【参考方案3】:在方法dismissView2中使用此代码
for (UIView *subview in view.subviews )
[subview removeFromSuperview];
【讨论】:
以上是关于从 UIViewController 中删除子视图的主要内容,如果未能解决你的问题,请参考以下文章
UIView:将 UIViewController 的视图添加为子视图并将其删除
将 NSArray 从 UIViewController 传递到子视图 [重复]