[英]Make a UIView change its superview (or superview's superview)?
I have four UIView
s: viewA
is the root view, vhich has viewB
as its subview. 我有四个
UIView
: viewA
是根视图, viewB
具有viewB
作为其子视图。 viewB
has viewC
as its subview and viewC
has viewD
as its subview. viewB
有viewC
作为其子视图和viewC
已viewD
作为其子视图。 So there is a hierarchy of four views. 因此,存在四个视图的层次结构。
viewD
is basically a button that can be tapped to do something. viewD
基本上是一个可以点击以执行某项操作的按钮。 Because the operation has to do with database, it may takes a while. 由于该操作与数据库有关,因此可能需要一段时间。 So I want a dark opaque
UIView
with a UIActivityIndicator
at the center to tell the user to wait for a moment and prevent other tapping action that may cause trouble. 因此,我想要一个带有
UIActivityIndicator
不透明深色UIView
,以告诉用户稍等片刻,并防止其他可能引起麻烦的轻击操作。 However, the dark view has to be put in viewA
to make it cover all the screen. 但是,必须将暗视图放入
viewA
,以使其覆盖整个屏幕。 Here are what I tried (and succeeded) but I want to know whether there are any easier ways to do it. 这是我尝试(并成功)的方法,但我想知道是否有更简单的方法可以做到这一点。
Access viewA
from viewD
by viewD.superview.superview.superview
. 通过
viewA
从viewD.superview.superview.superview
访问viewD
。 However it's not very flexible because once the hierarchy is changed, this chain is likely to be changed. 但是,它不是非常灵活,因为一旦更改了层次结构,此链就可能会更改。
Use delegation. 使用委托。
viewD.delegate = viewA
. viewD.delegate = viewA
。 However, viewD
is created by viewC
(its direct superview) which is created by viewB
, which is created by viewA
. 然而,
viewD
通过创建viewC
其通过创建(其直接上海华) viewB
,其通过创建viewA
。 So if viewD
, viewC
, and viewB
are not @property
of its respective superclass (accessing by viewA.viewB.viewC.viewD
is not possible), then viewA
cannot have a pointer to viewD
, which will make the delegation impossible (as far as I understand). 所以,如果
viewD
, viewC
和viewB
不@property
其各自的超类(通过访问viewA.viewB.viewC.viewD
是不可能的),那么viewA
不能有一个指针viewD
,这将使代表团不可能的(据我明白)。
Use NSNotification
. 使用
NSNotification
。 Make viewD
post a @"ActivityIndicatorShouldBePutOn"
notification and make viewA
an observer of it. 使
viewD
发布@"ActivityIndicatorShouldBePutOn"
通知,并使viewA
成为它的观察者。 However, NSNotification
is used for broadcasting right? 但是,
NSNotification
用于广播吗? So it is somewhat inappropriate. 因此有点不合适。
As long as this case goes (the dark mask is put on the whole screen), I can use something related to UIApplication
to access the screen and put it direct onto it. 只要这种情况得以解决(在整个屏幕上都放置了黑色蒙版),我就可以使用与
UIApplication
相关的内容来访问屏幕并将其直接放在屏幕上。 But if I want to put it only on a part of a screen for a different purpose, then this is not going to work. 但是,如果我只想出于其他目的将其放在屏幕的一部分上,那么它将无法正常工作。
So I want to ask whether there is a better way of doing it or whether one method mentioned above is the standard way. 因此,我想问一问是否有更好的方法或者上述一种方法是否是标准方法。
You can add the activity indicator view to the main window. 您可以将活动指示器视图添加到主窗口。 You can get the keyWindow using:
您可以使用以下方法获取keyWindow:
[[UIApplication sharedApplication] keyWindow];
I would recommend this API MBProgressHUD . 我会推荐此API MBProgressHUD 。 It has a good look and feel and is dead easy to use:
它具有良好的外观和感觉,并且易于使用:
Displaying the activityIndicator 显示活动指示器
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
Removing it 移除它
[MBProgressHUD hideHUDForView:self.view animated:YES];
you can use option 3 even though its a broadcast, you can listen to it with only 1 observer. 您可以使用选项3,即使它是广播节目,也只能由1个观察者收听。 then just have different notifications for each UIView
然后每个UIView都有不同的通知
I would say that is the easiest/guaranteed fix even though it may not be elegant, nothing wrong with it imo. 我会说这是最简单/有保证的修复,即使它可能不够优雅,也没有错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.