2

I am trying to determine what is the best practice for sending data from a UISplitViews Master to it's Detail. I want to try to avoid importing headers and make the code as reusable as possible. I feel like there should be a really good way of doing this, but the best I can think of is to declare a protocol, but sometimes protocols can get a bit messy IMHO. and talking to the detail by using

[self setDelegate:id<myProtocol>)self.splitViewController.viewControllers objectAtIndex:1]];

Seems kind of flaky

Any Ideas?

Weston
  • 1,481
  • 1
  • 11
  • 31

2 Answers2

5

If you don't want to import each other headers and dont want to use a delegate, the only thing I can think of is using Notifications:

Send and receive messages through NSNotificationCenter in Objective-C?

You can pass information in a dictionary to the notification

Community
  • 1
  • 1
Antonio MG
  • 20,382
  • 3
  • 43
  • 62
0

You could just go ahead and use your appDelegate to hold different variables and access them from your viewcontrollers. But I would recommend creating a new Singleton class which will do that for you.

For example, create a class called e.g. Holder.h + .m and declare all the needed variables.

.h:

@property (nonatomic, strong) NSArray *someArray;

.m

+(id)sharedHolder{
static Holder *holder = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    holder = [[self alloc] init];
});
return holder;

}

now you can set your variable by doing [[Holder sharedHolder] setSomeArray:anotherArray] and then read it from somewhere else like this: [[Holder sharedHolder] someArray]

EDIT: You could also use Antonio's approach with NSNotification

gasparuff
  • 2,295
  • 29
  • 48