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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:42:58  来源:igfitidea点击:

Swift: Reload a View Controller

iosswiftrefreshviewcontroller

提问by Matt Spoon

I need to refresh a view controllerwhen 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 controllerthen come back to it, it seems to work perfectly?

view controller当点击某个按钮时,我需要刷新。简单地激活viewDidLoad()似乎对我需要做的事情不起作用。但是,当我离开view controller然后再回来时,它似乎完美地工作?

How can I refresh/reload a view controlleras 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

If you are using a navigation controller you can segue again to the current UIViewController and it will be refreshed. Here I use a UIButton for refreshing

如果您使用的是导航控制器,您可以再次切换到当前的 UIViewController,它会被刷新。 这里我使用了一个 UIButton 来刷新

回答by dimaskpz

View Life Cycle

查看生命周期

it will load ViewWillAppeareverytime 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()——在视图控制器的内容视图添加到应用程序的视图层次结构之前调用。使用此方法触发在内容视图显示在屏幕上之前需要发生的任何操作。尽管名称如此,但仅仅因为系统调用了此方法,并不能保证内容视图将变为可见。该视图可能被其他视图遮挡或隐藏。此方法仅指示内容视图即将添加到应用程序的视图层次结构中。

https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html

https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html

回答by ballercoder1

If you need to update the canvas by redrawing views after some change, you should call setNeedsDisplay.

如果您需要在一些更改后通过重绘视图来更新画布,您应该调用 setNeedsDisplay。

Thank you @Vincent from an earlier comment.enter image description here

谢谢@Vincent 来自较早的评论。在此处输入图片说明