目前我有我的應用程序這樣的佈局。我有一個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