检查子视图是否在视图中
我正在制作一个应用程序,在 IBAction
上使用 addSubview:
将子视图添加到视图中。以同样的方式,当再次触摸带有该 IBAction
的按钮时,应在该 IBAction
上添加的子视图上调用 removeFromSuperview
:
PSEUDO代码
-(IBAction)showPopup:(id)sender
{
System_monitorAppDelegate *delegate = (System_monitorAppDelegate *)[[UIApplication sharedApplication] delegate];
UIView *rootView = delegate.window.rootViewController.view;
if([self popoverView] is not on rootView)
{
[rootView addSubview:[self popoverView]];
}
else
{
[[self popoverView] removeFromSuperview];
}
}
I'm making an app where I add a subview to a view using addSubview:
on an IBAction
. In the same way, when the button with that IBAction
is touched again should call removeFromSuperview
on that subview added on that IBAction
:
PSEUDO CODE
-(IBAction)showPopup:(id)sender
{
System_monitorAppDelegate *delegate = (System_monitorAppDelegate *)[[UIApplication sharedApplication] delegate];
UIView *rootView = delegate.window.rootViewController.view;
if([self popoverView] is not on rootView)
{
[rootView addSubview:[self popoverView]];
}
else
{
[[self popoverView] removeFromSuperview];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可能正在寻找 UIView 的
-(BOOL)isDescendantOfView:(UIView *)view;
,取自 UIView 类参考。你最终会得到如下代码:
Objective-C
Swift 3
You are probably looking for UIView's
-(BOOL)isDescendantOfView:(UIView *)view;
taken in UIView class reference.You will end up with a code like :
Objective-C
Swift 3
试试这个:
Try this:
Swift 的等价物看起来像这样:
The Swift equivalent will look something like this:
检查子视图的超级视图...
Check the superview of the subview...
你的 if 条件应该像
Your if condition should go like
这里我们使用了两种不同的视图。父视图是我们在其中搜索后代视图并检查是否添加到父视图的视图。
Here we used two different views. Parent view is the view in which we are searching for descendant view and check wether added to parent view or not.