Swift退出登录功能

Swift退出登录功能

在xcode 11.4.1中,除了AppDelegate.swift之外,多了一个SceneDelegate.swift,我使用了UINavigationController()来实现的登录->主界面的跳转。

SceneDelegate.swift

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
    
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else {
    
     return }
        
        let view = ViewController()
        let nav = UINavigationController(rootViewController: view)
        window = UIWindow(windowScene: scene as! UIWindowScene)
        window?.rootViewController = nav
        window?.backgroundColor = .white
    }
}

在实现退出登录功能时,尝试:popToRootViewController没有效果:

// 没有效果
self.navigationController?.popToRootViewController(animated: false)

正确的做法应该是:

func onLogout(_act: UIAlertAction) {
    
    
        // self.navigationController?.popViewController(animated: false)
        // self.navigationController?.popToRootViewController(animated: false)
        // self.navigationController?.pushViewController(ViewController(), animated: false)
        
        let view = ViewController() // 这是登录view
        let nav = UINavigationController(rootViewController: view)
        self.view.window?.rootViewController = nav
        
        // 断开TCP连接
        _ = IMManager.singleton.loginManager.logout(callback: nil)
    }

特此记录。

参考:swift 登录界面跳转
https://blog.csdn.net/wangxiaosu0501/article/details/80642141?utm_source=blogxgwz0

猜你喜欢

转载自blog.csdn.net/xmcy001122/article/details/105623862