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 Build a Playlist Browser with Objective-C Working With Multiple View Controllers Modifying the UI

Grayson Swaim
Grayson Swaim
8,992 Points

destinationViewController

My question involves the "prepareforSegue" method in which we use the "destinationViewController" attribute to establish a connection between 2 view controllers:

PlayListDetailViewController *playListDetail = (PlayListDetailViewController *)segue.destinationViewController;

What kind of value is the "destinationViewController" returning, and what is the easiest way to verify this?

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Grayson Swaim,

The value stored within destinationViewController will always be an instance of the subclass UIView, we typically don't need to verify this as all views should be based upon it, however, if you wanted to for instance confirm it's an instance of PlayListDetailViewController before down-casting it.

We can do this by using the isMemberOfClass method which is always available to UIView instances.

UIView *view = segue.destinationViewController;

if ([view isMemberOfClass:[PlayListDetailViewController class]]) {
  // view is an instance of `PlayListDetailViewController`
} else {
  // view is NOT an instance of `PlayListDetailViewController`
}

Hope that helps.

Iulian Popescu
Iulian Popescu
1,363 Points

From Apple doc you can see that destinationViewController type is AnyObject in Swift and id in Objective-C. So, if you want to use it in prepareForSegue you can downcast it to your own view controller and configure it how you want. Good luck!