Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

iOS Foundations Deep Dive, Delegation video

I am implementing an app similar to that in the video where a view controller segue to another modal view controller , on closing the modal view controller I want to pass a date object back to the original view controller.

I have implemented the delegate as shown in the video, however when I run the app, I get an error stating that topViewController is an unrecognized selector. I believe I am getting this error because of changes made in iOS since the video was created. Can anyone provide updated code for the prepareForSegue function that will allow the delegate to work.

2 Answers

Hi thanks for responding. I found the issue, I was not setting the delegate when the presented view was being called. What I had to do was get the destination view controller and set the delegate like this [segue.destinationViewController setDelegate:self]

thanks again

Chris McKnight
Chris McKnight
Courses Plus Student 11,045 Points

Correct, prepareForSegue is where you would set delegates and properties on the view controller you are segueing to.

Chris McKnight
PLUS
Chris McKnight
Courses Plus Student 11,045 Points

Be sure you are calling topViewController on an instance of UINavigationController and you have this navigation controller setup in the storyboard (i.e. the segue arrow is pointing to the nav controller). When I use prepareForSegue with a nav controller I do something like this.

UINavigationController *navVC = (UINavigationController *)[segue destinationViewController];
// Note: [navVC topViewController] can also be used here instead
DetailViewController *detailVC = (DetailViewController *)[navVC.viewControllers lastObject];