mluton / EmbeddedSwapping

Demonstration of how to make a custom container view controller manage multiple child view controllers using storyboards.
MIT License
208 stars 30 forks source link

Adding a delegate with multiple VCs #8

Closed NickBols closed 10 years ago

NickBols commented 10 years ago

I've been trying to alter your example to work with multiple buttons and VCs. (much like your 3 buttons branch, I just discovered) So far so good, I can select 5 different views with different buttons without issue. However now I'm trying to set each VC's delegate to the main view controller (the one before the container view controller) so they can send data to it.

I've not been able to get this to work for some reason... Do you have any ideas on how to reach it?

I know it can normally be set like so:

myViewController *destination = [segue destinationviewcontroller] destination.delegate = self

But since your example uses a previously allocated instance like so:

self.firstViewController = segue.destinationviewcontroller

I can't seem to assign its delegate in any way... I must be overseeing something.

Thanks in advance!

NickBols commented 10 years ago

Okay, turns out to be this for already instantiated VCs:

self.firstViewController = segue.destinationViewController; self.firstViewController.delegate = (myMainViewController *)self.parentViewController;

However for the first screen (the one that has a "load if first time" else clause) the delegate is only being set when I open another VC first and then come back to it... Same code doesn't seem to do it yet.

NickBols commented 10 years ago

Still interested in knowing how to reach the VC at creation, but for now after hours of trying I've decided to implement a little workaround trick by re-performing the segue in the viewWillAppear method. Can't be seen by the end user and the delegate fires..

}

}

mluton commented 10 years ago

I'm glad you found a workaround for the time being. It may be a couple of days before I can extract myself from my current responsibilities to look into this. It sounds like a natural thing to want to do so I'm interested in giving it a shot myself. :smiley:

mluton commented 10 years ago

Looking at the three_button branch I was able to set the delete of all three of the contained view controllers to self like so.

if (([segue.identifier isEqualToString:SegueIdentifierFirst]) && !self.firstViewController) {
    NSLog(@"instantiating first view controller");
    self.firstViewController = segue.destinationViewController;
    self.firstViewController.delegate = self;
}

if (([segue.identifier isEqualToString:SegueIdentifierSecond]) && !self.secondViewController) {
    NSLog(@"instantiating second view controller");
    self.secondViewController = segue.destinationViewController;
    self.secondViewController.delegate = self;
}

if (([segue.identifier isEqualToString:SegueIdentifierThird]) && !self.thirdViewController) {
    NSLog(@"instantiating third view controller");
    self.thirdViewController = segue.destinationViewController;
    self.thirdViewController.delegate = self;
}

Because we call [self performSegueWithIdentifier:self.currentSegueIdentifier sender:nil]; in viewDidLoad on the container we end up running the first segue on startup. I'll try to clean up my experimental code and post to a separate branch.

NickBols commented 10 years ago

cool man, thanks for the quick response! Very clean coding, I like it :)

I actually set the delegates at the same places, but I still had to re-fire the first VC in viewWillAppear to make it work for some weird reason.

I think it might have something to do with the way you assigned your viewControllers to the delegate protocol. Either that or I made a small error somewhere by expanding on the standard embeddedSwapping project. (I only noticed there was a 3 VC branch after making it so it looks a little bit different)

Cheers!