当按“返回"时,如何在 navigationController 中重新加载 rootViewController? [英] How to reload rootViewController in navigationController When press "back"?
问题描述
一个 navigationController 中有 2 个 viewController:navigationController->root->A在viewController A中,如果用户进行一些设置并按下左栏按钮项目(返回),我希望根视图更新其布局(某些视图的大小将被更改).
There are 2 viewControllers in a navigationController: navigationController->root->A In viewController A, if a user make some settings and press the left bar button item(Back), I want the root view to renew its layout(some views' size will be changed).
现在,我通过在两个 viewControllers 之间添加一个更多的 navigationController(以模态呈现)来使它工作:navigationController->root->navigationController->A.
By now, I make it works by adding one more navigationController between the two viewControllers(present modally): navigationController->root->navigationController->A.
有没有办法用一个导航控制器更新根视图控制器?(截图 1)
Is there a way to renew the root viewController with one navigationController?(Screenshot 1)
谢谢.
----- 已编辑-----
----- Edited -----
示例代码:
override func viewWillAppear(_ animated: Bool) {
creatButtons()
}
func createButtons(){
let button1 = UIButton()
........
let button2 = UIButton()
........
.......
}
如果我在viewWillAppear()下创建了16个按钮,从A回来的时候所有的按钮都会被复制吗?它们的大小都需要更新.
If I create 16 buttons under viewWillAppear(), will all the buttons be duplicated when comes back from A? Their size are all need to be renewed.
推荐答案
不确定我是否正确理解您的问题,但这里有两种关于如何处理这种情况的替代方案:
Not sure if I understood your question correctly but here are two alternatives on how to handle this scenario:
如果您想在 A
中更改设置时刷新 root
的视图层次结构,只需确保将更改保留在 root
和 A
可以访问.覆盖root
中的-viewWillAppear:
(也会在返回"时触发)方法,每次都根据设置布局视图.
If you want to refresh the view hierarchy of root
when settings are changed in A
just make sure to persist the changes in a place that both root
and A
can access. Override the -viewWillAppear:
(will be triggered on "back" as well) method in root
and layout the view according to the settings every time.
其他选择:
您可以为由 root
实现的 A
创建一个 delegate protocol
并将 root
分配为 >A
的 delegate
当 root
实例化或呈现 A
时.
You could create a delegate protocol
for A
that is implemented by root
and assign root
as A
's delegate
when root
instantiates or presents A
.
A
然后会调用它的委托 (root
) 来通知它更改并让 root
更新它的视图.
A
would then invoke its delegate (root
) to inform it about the change and let root
update its views.
这篇关于当按“返回"时,如何在 navigationController 中重新加载 rootViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!