简体   繁体   English

SWIFTUI - 重新打开应用程序后如何导航到最后一个视图?

[英]SWIFTUI - How to navigate to the last view after reopening the app?

I am developing a text base rpg game.我正在开发一个基于文本的 rpg 游戏。 There will be a lot of number of views;会有很多浏览量; after every decision the user will navigate to a new view.在每个决定之后,用户将导航到新视图。 But if the user closes the app and reopens it, they would lose all the progress and start from the main view.但是如果用户关闭应用程序并重新打开它,他们将丢失所有进度并从主视图开始。

Is there a way to create a "Continue" button to take the user to the last view they were on?有没有办法创建一个“继续”按钮将用户带到他们上一个视图? Or Is there a way to open the last view directly when they reopen the app?或者有没有办法在他们重新打开应用程序时直接打开最后一个视图?

For example, the user will start from this:例如,用户将从这里开始:

struct ContentView: View {
var body: some View {

NavigationLink(destination: View1()) {

    Text(Start)

}
}
}

After playing for a while the user in on View28玩了一会儿后,用户在 View28 上

struct View28: View {
var body: some View {

NavigationLink(destination: View29()) {

    Text(Do This)

}

NavigationLink(destination: View30()) {

    Text(Do That)

}
}
}

The user closes the game, reopens it, and he/she is on the ContentView again.用户关闭游戏,重新打开它,然后他/她又在 ContentView 上。

struct ContentView: View {
var body: some View {

NavigationLink(destination: View1()) {

    Text(To View1)

}
NavigationLink(destination: LastViewedView()) {

    Text(Continue)

}
}
}

Is there a way to add a "Continue" button like above to take the user to the View28?有没有办法像上面一样添加“继续”按钮以将用户带到 View28?

Or is there a way to program the app to reopen View28 directly?或者有没有办法让应用程序直接重新打开 View28?

Thanks.谢谢。

After @nicksarno's suggestion I think I found a primitive way to solve the problem.在@nicksarno 的建议之后,我想我找到了一种解决问题的原始方法。

I created a global variable我创建了一个全局变量

  var ViewCounter = 0

And after every view, I added this code:在每次查看后,我添加了以下代码:

        // ViewCounter will take the value of which view it is on, and UserDefaults will save the value.
      .onAppear() {  
ViewCounter = 28
        UserDefaults.standard.set(ViewCounter, forKey: "Save1")
    }

And in the ContentView, first I retrieve the value for ViewCounter:在 ContentView 中,首先我检索 ViewCounter 的值:

onAppear() {

guard let retrievedmsg1 = UserDefaults.standard.value(forKey: "Save1") else {return}
                ViewCounter = retrievedmsg1 as! Int
}

Then I created a state variable and created a function to match it with ViewCounter:然后我创建了一个 state 变量并创建了一个 function 以将其与 ViewCounter 匹配:

@State var SelfViewCounter = 0

func ViewCountCheck() {
        
        SelfViewCounter = ViewCounter
    }

Under onAppear, I called the function:在 onAppear 下,我调用了 function:

self.ViewCountCheck()

And finally, I created a start button and a navigation button conditions:最后,我创建了一个开始按钮和一个导航按钮条件:

// This is to start the game
NavigationLink(destination: View1()) {
        Text("Start")
                 }

if SelfViewCounter == 1 {
        NavigationLink(destination: View1()) {
        Text("Continue")
        }
        } else if SelfViewCounter == 2 {
        NavigationLink(destination: View2()) {
        Text("Continue")
        }
        ...

I know it needs a lot of hardcoding and would take a lot of time to implement.我知道它需要大量的硬编码并且需要很多时间来实现。 But it works.但它有效。 I am pretty sure there needs to be a better way than this, so please add on.我很确定需要比这更好的方法,所以请补充。 Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM