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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值