-2

Hi im developing an app that has a parent view that then used containers to embed other views as seen below.

enter image description here

For now im only working with the left and centre container which are both table views. The main view or the Project screen view is my parent controller and i want it to pass data to and from the two child controller and i know for this the best option is to use delegates. However each example i have looked at that uses delegates, created and initialises a new view controller so for example lets say the left container embeds a view using the leftviewcontroller. Each example has this line of code.

LeftViewController *sampleProtocol = [[LeftViewController alloc]init];
LeftViewController.delegate = self;

Im thinking i dont need to create a new LeftViewController since it is embeded it is already in my list of child controllers. So my queston is how would i get the controller from the list of child controllers and set the parent as the delegate. I know i it is an array and i can use objectAtIndex but how do i know the order of items in the array will not change can i not call it but a tag or identifier? Thank you for any help sorry if the question is not that clear its my first time setting up delegates.

Wain
  • 118,658
  • 15
  • 128
  • 151
Tony_89
  • 797
  • 11
  • 39
  • Don't use the index, add a specific outlet / property and set the controller into that to use. – Wain May 20 '15 at 12:18
  • For something with so many data views, I would create a shared instance central data manager class which handles all your data. You then make each view controller work with a set of data from your data manager. Allow controllers to register for update notifications from the data manager instance. When a controller changes something, on committing the change all registered interested controllers will be notified and can decide what that change means to them. The API for each controller is then reduced to simply providing hooks to inject which data items they are a view/controller onto. – Rory McKinnel May 20 '15 at 13:01

3 Answers3

1

i know for this the best option is to use delegates.

In this case, I wouldn't be so sure. I think the best option would be to have a robust model and use KVO and notifications to signal updates between view controllers.


The direct answer to your question is not too bad.

for (UIViewController *viewController in self.childViewControllers) {
    if ([viewController isKindOfClass:[LeftViewController class]]) {
        LeftViewController *leftViewController = (id)viewController;
        leftViewController.delegate = self;
        break;
    }
}

I think a minor improvement on this would be to use the segue. Make sure each of the containers have a named segue. In this example, the left view controller has a segue with the identifier "Load Child LeftViewController".

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Load Child LeftViewController"]) {
        LeftViewController *leftViewController = segue.destinationViewController;
        leftViewController.delefate = self;
    }
}
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
-1
Its always better to use NSNotificationCenter for such complex mechanism.

*** put following code in LeftController.m ***

// *** Register a Notification to recieve a Data when something happens in Center controller ***
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receivedNotification:)
                                             name:@"hasSomeData"
                                           object:nil];

// *** create a method to receive Notification data ***
- (void)receivedNotification:(NSNotification *) notification {
    if ([[notification name] isEqualToString:@"hasSomeData"])
    {
        // do your stuff here with data
        NSLog(@"data %@",[notification object]);
    }
}

*** when something happen in center controller post a notification to inform Left Controller ***

[[NSNotificationCenter defaultCenter] postNotificationName:@"hasSomeData" object:self];
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
-1
    //Secondvc.h
    @protocol Sendmessage<NSObject>
    @required
    -(void)Object:(NSArray *)tosend;
    @end
    @interface Secondvc:UIViewcontroller{
    id <Sendmessage> delegate;
    }
    @property(strong,nonatomic) id <Sendmessage> delegate;
    @end

    //Secondvc.m
    @implementation Secondvc
    @synthesize delegate;
    -(void)viewDidLoad{
    //Do Something here!

    }
    //Pass Some Value When a button event occured in Second vc
    -(IBAction)Send_Data{
    [self dismissViewControllerAnimated:Yes completion:nil];
    [self.delegate Object:[NSArray Arraywithobjects:@"Hello",nil]];
    }
    @end


    //FirstVc.h
    #import "Secondvc.h"
    @interface FirstVc<Sendmessage>
    @end

    //FirstVc.m
    @implementation FirstVc
    -(void)viewDidLoad{
    Secondvc* Object=[[Secondvc alloc]init];
    Object.delegate=self;
    }
    #pragma mark Secondvc Deklegate method implementation
    -(void)Object:(NSArray *)tosend{
    NSLog(@"Recieved data Form Second VC Is:\n%@",tosend);
    }
    @end

HTH!Enjoy Coding.

Naresh Reddy M
  • 1,096
  • 1
  • 10
  • 27