gpt4 book ai didi

ios - swift4 : how to check if user logged befor tab bar item clicked

转载 作者:行者123 更新时间:2023-11-28 14:40:10 25 4
gpt4 key购买 nike

这个问题以前有人问过,也有人回答过,我的问题不是唯一的,但一定有什么遗漏。我只是想检查用户之前是否登录过应用程序,因为他的数据存储在 UserDefaults 中,但这对我不起作用,这是我的 TabBarViewController

class TabViewController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()

self.delegate = self
}
// UITabBarDelegate
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("Selected item")
}

// UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if(viewController is MessagesViewController) {
print("trueee")
if(UserStorage.id == "") {
Toast.toast(messsage: "not loggoed user", view: self.view)
}
} else {
print("faaaaaaaalse")
}
print("Selected view controller")
}
}

我想检查用户是否打开 MessageViewController 然后如果用户登录打印一些东西但它总是打印 faaaaaaaalse

note: Toast.toast() is a function i created to show toast and UserStorage.id returns user id stored in USerDefaults

这是显示我的结构的图像: this is image which shows my structure我该怎么办?

最佳答案

只是你的标签栏的根 Controller 是 2 个导航 Controller ,而不是 MessageViewController。首先,对于 tabBarController,您必须找到包含 MessageViewController 的 navigationController,而不是在此导航中找到所需的 ViewController。

所以我有适合您的解决方案:

import UIKit

class TabbarController: UITabBarController, UITabBarControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
// for tab bar initialization
if let viewControllers = self.viewControllers,
viewControllers.count >= 1,
// the index of viewController is 0 here, but if your tab bar's started controller is not 0 you can set yours
let navigationController = viewControllers[0] as? UINavigationController {
for controller in navigationController.viewControllers {
if let messagesViewController = controller as? MessagesViewController {
doWithMessagesViewControllerWhatYouWant(_viewController: messagesViewController)
}
}
}
}

// UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if let navigationController = viewController as? UINavigationController{
for controller in navigationController.viewControllers {
if let messagesViewController = controller as? MessagesViewController {
doWithMessagesViewControllerWhatYouWant(_viewController: messagesViewController)
}
}
}
}

private func doWithMessagesViewControllerWhatYouWant(_viewController: MessagesViewController) {
print("do some operations with messagesViewController")
if(UserStorage.id == "") {
Toast.toast(messsage: "not loggoed user", view: self.view)
}
}

}

关于ios - swift4 : how to check if user logged befor tab bar item clicked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50582334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com