ios - 打开第二 View

标签 ios swift

我尝试检查用户是否已成功登录。如果这是真的,则不会关闭登录屏幕。 TerminView 将被加载。 enter image description here

这是 LoginViewController 中的函数(我也想使用这个函数,当用户在按下“登录”按钮后成功登录时)

func checkIfLoggedIn() -> Void {

    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
    let url = NSURL(fileURLWithPath: path)
    let filePath = url.appendingPathComponent(K.login.file)?.path
    let fileManager = FileManager.default
    if fileManager.fileExists(atPath: filePath!) {
        self.navigationController!.pushViewController(self.storyboard!.instantiateViewController(withIdentifier: "TerminView") as UIViewController, animated: true)

    }
}

在 Storyboard 中,我还为 TerminViewController 指定了 id:TerminView

这是在 AppDelegate 中调用这个函数:

func applicationDidFinishLaunching(_ application: UIApplication) {

        LoginViewController().checkIfLoggedIn()
}

我的错误在哪里?

最佳答案

试试这个

您需要更改带有登录标志的 rootViewController

func isUserLoggedIN() -> Bool {
        var struserID: String? = ""
        if  UserDefaults.standard.object(forKey:"LoginKey") != nil {
            struserID  = UserDefaults.standard.object(forKey:"LoginKey") as! String?
        }
        return struserID!.characters.count > 0 ? true : false
    }

检查如下

if self.isUserLoggedIN() {
     let navLog: UINavigationController? = Constant.StoryBoard.instantiateViewController(withIdentifier: "loginNavBar") as? UINavigationController
     window?.rootViewController = navLog
            }
else{
   // set another ViewController
}

关于ios - 打开第二 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44157428/

相关文章:

ios - Microsoft Face API - 错误 401,iOS

ios - 尝试存档时自定义 NavigationStack 导致 `error: Segmentation fault: 11` 时的解决方法建议?

ios - CNContactViewController() 的 "Create New Contact"和 "Add to Existing Contact"

ios - 如何在 map View 上设置填充 (Mapbox-ios-sdk)

ios - 滑动以查看 UITableViewCells 崩溃时的操作

ios - 如何在导航 Controller 标题中设置副标题?

ios - Twitter/Fabric 登录按钮未按预期运行

iphone - "iOS threading programming guide"中的条件样本

ios - 核心数据: bundle.main.url Thread 1 fatal error

swift - Couchbase 接收 channel 空