xcode 一种在每次运行 swift 时重新加载 ViewDidLoad 的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32166593/
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-09-15 07:42:59  来源:igfitidea点击:

A way to reload the ViewDidLoad on each run swift

iosiphonexcodeswiftviewdidload

提问by Starlord

I'm making this small game with swift and the game is set up using the viewDidLoad. Each day the game is suppose to be slightly different, but that depends on that the viewDidLoad runs. So let's say a user plays at day 1 and keeps the game active to day 2, the game won't function on day 2 since the viewDidLoad wasn't run. For the game to function properly I need to run the VieDidLoad each time "applicationDidBecomeActive", to make sure it runs as it should.

我正在用 swift 制作这个小游戏,游戏是使用 viewDidLoad 设置的。假设游戏每天都略有不同,但这取决于 viewDidLoad 的运行情况。因此,假设用户在第 1 天玩游戏并将游戏保持活动到第 2 天,由于未运行 viewDidLoad,游戏将无法在第 2 天运行。为了让游戏正常运行,我需要在每次“applicationDidBecomeActive”时运行 VieDidLoad,以确保它按预期运行。

I've tried to do like this:

我试过这样做:

func applicationDidBecomeActive(application: UIApplication) 
{
myViewController?.viewDidLoad()
}

I also tried to wrap my code that is inside viewDidLoad like this and run it in applicationDidBecomeActive. But that doesn't do it.

我还尝试像这样将 viewDidLoad 中的代码包装起来,并在 applicationDidBecomeActive 中运行它。但这不行。

override func viewDidLoad()
{
   func refreshView()
   {
    //all the code goes here
    }
}

Any idea what I could do?

知道我能做什么吗?

回答by Pablo A.

Set your view controller to listen applicationDidBecomeActivenotifications and run the configuration method when it receives it

将您的视图控制器设置为侦听applicationDidBecomeActive通知并在收到通知时运行配置方法

NSNotificationCenter.defaultCenter().addObserver(
    self,
    selector: "refreshView:",
    name: UIApplicationDidBecomeActiveNotification,
    object: nil)

回答by Adnan Aftab

viewDidLoadmethod not meant to load many times, this will be called only once per lifecycle of viewControllerwhen view first created an loads into memory, and this is not right approach call this again and again.

viewDidLoad方法不打算多次加载,viewController在视图首次创建加载到内存中的每个生命周期中只会调用一次,这不是正确的方法,一次又一次地调用它。

What you want is possible without loading viewDidLoadagain and again, move your refreshViewmethod out of viewDidLoadand call it when required, you can call it once form viewDidLoadand after that you can call it again when required.

你想要的东西不需要viewDidLoad一次又一次地加载,将你的refreshView方法移出viewDidLoad并在需要时调用它,你可以调用一次表单viewDidLoad,之后你可以在需要时再次调用它。

You can respond to application notificationmethods like UIApplicationWillEnterForegroundNotificationor in UIApplicationDidBecomeActiveNotificationand then figure out do you want to refresh view or not, if you want to refresh view just call your refresViewmethod.

您可以响应notification诸如UIApplicationWillEnterForegroundNotification或 in 之 类的应用程序方法 UIApplicationDidBecomeActiveNotification,然后确定是否要刷新视图,如果要刷新视图,只需调用您的refresView方法。

Your code should look like this

你的代码应该是这样的

override func viewDidLoad()
{
   super.viewDidLoad()
   NSNotificationCenter.defaultCenter().addObserver(self, selector: "OnAppBecameActive", name: UIApplicationDidBecomeActiveNotification, object: nil)

   refreshView()
}

func refreshView()
{
    //all the code goes here
}

And add this method as well

并添加此方法

func OnAppBecameActive()
{
// Add logic here to check if need to call refresh view method
if needToRefreshView
{
  refreshView()
}
}

回答by Abdelrahman Mohamed

I have the same probelm but i solve it with call

我有相同的问题,但我通过电话解决了

self.viewDidLoad()

On my Func I wanna to call my view did load again from it.

在我的 Func 上,我想调用我的视图并从它再次加载。