Associating a Realm Object with a UITabBarController in iOS 9 with RealmSwift

时间:2015-09-01 22:53:26

标签: ios swift swift2 realm ios9

Below is a screenshot of the storyboard for my project:

Realm UITabBarController Project

As you can see, I have a UIViewController with a UITableView embedded in a UINavigationController. When the user adds an item to the UITableView a new Realm Object model which I have named Project is created (I'm using RealmSwift) and they are taken to the first item in the UITabBarController. I'm wondering if it is possible to associate the UITabBarController and all of its child view controllers with the Project that is created.

Here is the code that creates the Object (which is working fine):

    func add(withName projectName: String) {
        let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
        dispatch_async(queue) {
            autoreleasepool {
                let realm = try! Realm()
                let newProject = Project(value: ["name": projectName, "date": NSDate()])
                realm.write({ () -> Void in
                    realm.add(newProject)
                })
            }
        }
    }

   override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

        selectedProject = array[indexPath.row]
        print(selectedProject)
        self.performSegueWithIdentifier("showTab", sender: self)
    }

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showTab" {
            let dvc = segue.destinationViewController as! TabBarViewController
                dvc.project = selectedProject

        }
    }

As you can see, I'm passing the new Project to the UITabBarController.

Here is my code in the UITabBarController:

import UIKit
import RealmSwift

class MasterTabViewController: UITabBarController {

    var project: Project?

    override func viewDidLoad() {
        super.viewDidLoad()
        configureNavigationBar()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    }

    // MARK: - UI Styling
    private extension MasterTabViewController {
        func configureNavigationBar() {
            navigationItem.title = project?.name
        }

    }   

This sets the title of all UIViewControllers that descend from the UITabBarController.

Is there a way for me to make that Project object available to all view controllers that descend from the UITabBarController? I haven't been able to find too much information on this. One suggestion that I found was to store the Object in NSUserDefaults but it seems to me that there must be a better way.

EDIT: I forgot to add that I can think of a way to do this with Core Data using NSManagedObjectContext, but I'm having some trouble seeing the equivalent in Realm.

1 个答案:

答案 0 :(得分:1)

你走在正确的轨道上!当视图控制器是UITabBarController的子级时,您可以使用self.tabBarController属性从子视图控制器内部访问选项卡栏控制器。

由于您知道这些视图控制器是UITabBarController的子类的子节点,它也提供Project作为属性,因此应该只需调用该选项卡栏控制器属性,对结果视图进行类型转换控制器到标签栏控制器子类,然后调用它上面的project属性。

所以,在你的任何一个孩子控制器中:

let project = (self.tabBarController as MasterTabViewController).project?

您可以通过使project成为自动调用此行代码的子控制器类的只读属性来轻松优化此操作。祝你好运!