简体   繁体   中英

Reference a property in another unrelated View Controller

I have a question regarding best practices for Cocoa application architecture. If you look at the screenshot of my app's storyboard there is a view controller in the bottom left (which many controls) and there are two view controllers with table views in a split view controller, on the right side. I need to reference array controllers that are in each of the table view controllers (right side ) inside the controls view controller (left side).

How do I reach those array controllers all the way through the view hierarchy (which goes through different container views, etc.)?

I could of course just refer them in my app delegate, which is a singleton, and get them from there but we all know that this is not good OOP architecture.

在此处输入图片说明

You can create your own Singleton Class for that. Like AppNameDataManager then create properties and set them from the view controller where you have to set and get in view controller where you have to get.

#define SINGLETON_FOR_CLASS(classname)\
+ (id) sharedManager {
    static dispatch_once_t pred = 0;\
    static id _sharedObject = nil;
    dispatch_once(&pred, ^{
    _sharedObject = [[self alloc] init];
    });\
    return _sharedObject;
}

just import that class and get instance by calling [ClassName sharedManager]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM