目前我有我的应用程序这样的布局。我有一个UITabBarcontroller作为我的根视图控制器内的应用程序委托,这是好的,并且效果很好。使用或不使用全局变量访问UITabBarController中的函数/变量?
@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)
然而,在我的布局选项卡控制器,我有一个办法在那里我有UITabBar顶部铺设出来,随后,规定了所有这样的意见的覆盖didload功能。
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]
但是,当我尝试从另一个viewcontroller里面访问一个函数或任何在这个tab控制器内的viewcontroller。这样
class settingsController: UIViewController{
messagesController().removeMessages()
}
.......
class messagesController: UIViewController{
removeMessages(){
messages.removeAll()
messagesDictionary.removeAll()
tableView.reloadData()
print("working")
}
}
的功能被调用(IM把它在上登出的功能),我得到我想要的影响上,打印的消息,但什么viewcontroller似乎不工作。目前,我已经尝试了这些方法(在设置控制器上)。
我知道这与swift中的tabbarcontroller有关,如何使视图内部的视图运行并行。
我做了什么,它的工作原理是在我的tabs.swift文件中的类之前放置了变量的声明。所以我拿出了设置视图的let语句,并在宣布课程之前并在导入语句下放置它们。这有效,但这是不好的做法,有没有更好的方法呢?因为这让声明现在是全球性的。
真的很感谢帮助家伙和我很抱歉的长篇文章,我只是想让你试着理解我在这里说什么,我是新的。
尝试之前打开每一个标签。也许你的视图控制器尚未加载。 – ObranS
如果之前运行的所有viewDidLoad方法都运行了,则表明您不在主线程中调用该方法。 – ObranS
@ObranS尝试了这个伴侣,这与它被声明有关,就像我可以通过让它们声明为全局变量(在类之外声明它们)来解决它,但我不确定这是否是不好的做法,什么不是。不过谢谢。 :) – zak