【发布时间】:2021-07-29 23:29:04
【问题描述】:
我正在为我在大学的计算课程开发一个 iOS 应用程序原型,我用它来证明导航和登录系统的功能。到目前为止,我在这方面取得了成功,一切都按计划进行,尽管我怀疑我是否以正确的方式完成了这件事,因为我还是 Swift 编程语言和 XCode IDE 的新手。
我的问题是,我知道这听起来很愚蠢,因为我的应用程序不允许我在转换到新视图之前有时间延迟。登录成功后,立即跳转到我的主页。但是,我希望应用在成功登录和打开主页之间有短暂的延迟。
我在搜索了不同的方法后尝试了这个方法,当前方法是sleep(UInt32(1.0))在打开主页之前暂停应用程序 1 秒 - 本节的完整代码如下:
// external if statement for when login succeeds
if authenticationDidSucceed {
Text("Login succeeded!")
.font(.headline)
.frame(width: 250, height: 80)
.background(Color.green)
.cornerRadius(20.0)
.foregroundColor(.white)
.animation(Animation.default)
// this is what's broken
sleep(UInt32(1.0))
// calling navigation bar containing other pages
HostingTabBar()
}
这似乎返回了错误Static method 'buildBlock' requires that 'UInt32' conform to 'View',我认为这与它在 if 语句中的事实有关。
如果有人知道解决此问题的任何方法,将不胜感激,如果您需要我附加更多代码 sn-ps 或其他任何内容。
非常感谢您的帮助。
【问题讨论】:
-
你在哪里将
authenticationDidSucceed设置为true? -
在你这样做的时候,你可以这样做
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { authenticationDidSucceed = true } -
它与 if 语句无关。你读过错误信息吗?睡眠不是你想象的那样。
-
@aheze authenticationDidSucceed 在单独的代码块中设置为true,用于按下登录按钮时,其中
if self.username == storedUsername && self.password == storedPassword,从这里设置为true。 -
@aheze 我已经添加了您建议的包含
authenticaionDidSucceed = true行的代码,它可以工作!现在程序在点击按钮后明显暂停了 1 秒钟,但它似乎没有显示“登录成功!”消息,尽管这对我来说可能是一个简单的解决方法。