目前我的应用程序有这样的布局。我有一个 UITabBarcontroller 作为应用程序委托(delegate)内的 Root View Controller ,这很好并且效果很好。
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FIRApp.configure()
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = Tabs()
UITabBar.appearance().tintColor = UIColor.init(r: 198, g: 214, b: 91)
UITabBar.appearance().barTintColor = UIColor.init(r: 47, g: 47, b: 47)
UINavigationBar.appearance().barTintColor = UIColor.init(r: 53, g: 57, b: 77)
UINavigationBar.appearance().tintColor = UIColor.init(r: 198, g: 214, b: 91)
但是,在我使用选项卡 Controller 的布局中,我已经将其布局为在顶部有 UITabBar,然后是一个覆盖 didload 函数,该函数会像这样布置所有 View 。
class Tabs: UITabBarController{
override func viewDidLoad() {
super.viewDidLoad()
tabBar.isTranslucent = true
let feed = feedController()
let feedArray = UINavigationController(rootViewController: feed)
let feedButton = UITabBarItem(title: nil, image: UIImage(named: "feed.png"), selectedImage: UIImage(named: "selectedimage.png"))
feedButton.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
feedArray.tabBarItem = feedButton
let messages = messagesController()
let messagesArray = UINavigationController(rootViewController: messages)
let messagesButton = UITabBarItem(title: nil, image: UIImage(named: "messages.png"), selectedImage: UIImage(named: "selectedimage.png"))
messagesButton.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
messagesArray.tabBarItem = messagesButton
let post = postController()
let postButton = UITabBarItem(title: nil, image: UIImage(named: "post.png"), selectedImage: UIImage(named: "selectedimage.png"))
postButton.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
post.tabBarItem = postButton
let profile = profileController()
let profileArray = UINavigationController(rootViewController: profile)
let profileButton = UITabBarItem(title: nil, image: UIImage(named: "profile.png"), selectedImage: UIImage(named: "selectedimage.png"))
profileButton.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
profile.tabBarItem = profileButton
let settings = settingsController()
let settingsArray = UINavigationController(rootViewController: settings)
let settingsButton = UITabBarItem(title: nil, image: UIImage(named: "settings.png"), selectedImage: UIImage(named: "selectedimage.png"))
settingsButton.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
settingsArray.tabBarItem = settingsButton
if FIRAuth.auth()?.currentUser?.uid == nil {
DispatchQueue.main.async {
self.handleLogout()}
}
/* all views BOII */ viewControllers = [feedArray, messagesArray, post, profileArray, settingsArray]
但是,当我尝试从另一个 View Controller 内部访问此选项卡 Controller 内的 View Controller 中的函数或其他内容时。像这样
class settingsController: UIViewController{
messagesController().removeMessages()
}
......
class messagesController: UIViewController{
removeMessages(){
messages.removeAll()
messagesDictionary.removeAll()
tableView.reloadData()
print("working")
}
}
函数被调用(我将其放入注销时的函数中),我收到打印消息,但我想要影响该 View Controller 的任何内容似乎都不起作用。目前,我已经尝试过这些方法(在设置 Controller 上)。
我知道这与 swift 中的 tabbarcontroller 有关,与内部 View 如何彼此并行运行有关。
我所做的并且有效,是将变量的声明放在 tabs.swift 文件中的类之前。因此,我采用了设置 View 的 let 语句,并将它们放在声明类之前和 import 语句下。这确实有效,但是这是不好的做法吗?有更好的方法吗?因为这个 let 语句现在是全局的。
真的很感谢大家的帮助,我很抱歉写了这么长的帖子,我只是想让你尝试理解我在这里所说的内容,我是新人。
最佳答案
如果您的 UIKit 对象从当前堆栈进行操作,它将无法工作。由于应该从主线程调用对象。
DispatchQueue.main.async {
self.handleLogout()
}
我只能建议您快速解决方案,对 UI 元素使用下一个 block :
dispatch_async(dispatch_get_main_queue()) {
messages.removeAll()
messagesDictionary.removeAll()
tableView.reloadData()
}
关于ios - 使用或不使用全局变量访问 UITabBarController 内的函数/变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44500948/