ios Swift:重新加载视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31207783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift: Reload a View Controller
提问by Matt Spoon
I need to refresh a view controller
when a certain button is tapped. Simply activating viewDidLoad()
does not seem to be working for what I need to do. However, when I leave the view controller
then come back to it, it seems to work perfectly?
view controller
当点击某个按钮时,我需要刷新。简单地激活viewDidLoad()
似乎对我需要做的事情不起作用。但是,当我离开view controller
然后再回来时,它似乎完美地工作?
How can I refresh/reload a view controller
as if I have left it then come back to it without ever actually leaving it?
我如何刷新/重新加载一个view controller
好像我已经离开它然后回到它而没有真正离开它?
回答by iAnurag
Whatever code you are writing in viewDidLoad
, Add that in viewWillappear()
. This will solve your problem.
无论您在编写什么代码,都将其viewDidLoad
添加到viewWillappear()
. 这将解决您的问题。
回答by Edward Anthony
You shouldn't call viewDidLoad method manually, Instead if you want to reload any data or any UI, you can use this:
你不应该手动调用 viewDidLoad 方法,相反,如果你想重新加载任何数据或任何 UI,你可以使用这个:
override func viewDidLoad() {
super.viewDidLoad();
let myButton = UIButton()
// When user touch myButton, we're going to call loadData method
myButton.addTarget(self, action: #selector(self.loadData), forControlEvents: .TouchUpInside)
// Load the data
self.loadData();
}
func loadData() {
// code to load data from network, and refresh the interface
tableView.reloadData()
}
Whenever you want to reload the data and refresh the interface, you can call self.loadData()
每当你想重新加载数据和刷新界面时,都可以调用self.loadData()
回答by Sandeep Vishwakarma
In Swift 4:
在 Swift 4 中:
self.view.layoutIfNeeded()
回答by Ahmed Khedr
This might be a little late, but did you try calling loadView()
?
这可能有点晚了,但你试过打电话loadView()
吗?
回答by Ben Spector
回答by dimaskpz
it will load ViewWillAppear
everytime you open the view. above is the link to the picture View Controller Lifecycle.
ViewWillAppear
每次打开视图时它都会加载。上面是图片View Controller Lifecycle的链接。
viewWillAppear()—Called just before the view controller's content view is added to the app's view hierarchy. Use this method to trigger any operations that need to occur before the content view is presented onscreen. Despite the name, just because the system calls this method, it does not guarantee that the content view will become visible. The view may be obscured by other views or hidden. This method simply indicates that the content view is about to be added to the app's view hierarchy.
viewWillAppear()——在视图控制器的内容视图添加到应用程序的视图层次结构之前调用。使用此方法触发在内容视图显示在屏幕上之前需要发生的任何操作。尽管名称如此,但仅仅因为系统调用了此方法,并不能保证内容视图将变为可见。该视图可能被其他视图遮挡或隐藏。此方法仅指示内容视图即将添加到应用程序的视图层次结构中。