使用或不使用全局变量访问UITabBarController中的函数/变量?

时间:2017-06-12 13:42:28

标签: ios swift 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")

}
}

函数被调用(我把它放在logout中的函数中),我得到了打印消息但是我想要的那个视图控制器上的任何东西似乎都不起作用。目前,我到目前为止已尝试过这些方法(在设置控制器上)。

我知道这与swift中的tabbarcontroller有关,关于内部视图如何相互平行。

我做了什么,它有用,是在我的tabs.swift文件中将变量的声明放在类之前。所以我接受了设置视图的let语句,并在声明类之前和import语句下放置它们。这有效,但这是一种不好的做法,还有更好的方法吗?因为这个语句现在是全局的。

非常感谢帮助人员,我很抱歉这篇长篇文章,我只是想让你试着理解我在这里说的话,我是新人。

1 个答案:

答案 0 :(得分:0)

如果您的UIKit对象从当前堆栈进行操作,它将无法正常工作。因为对象应该从主线程调用。

DispatchQueue.main.async {
    self.handleLogout()
}

我只能建议您快速使用下一个块来解决UI元素:

dispatch_async(dispatch_get_main_queue()) {
    messages.removeAll()
    messagesDictionary.removeAll()
    tableView.reloadData()
}
相关问题