Q:

Clean way to set delegate on custom class through topviewcontroller

I've defined a class NewViewController, which shows a screen to add a new item in the app, which is wrapped in a UINavigationController. I want to set a delegate on this UIViewController, what I am currently doing in this way in the prepareForSegue method:
UINavigationController *navController = segue.destinationViewController;
NewViewController *newViewController = (NewViewController *)navController.topViewController;
[newViewController setDelegate:self];

This works, but I have a feeling this is not elegant and not how I'm supposed to do this. What is the best approach to get this result?
Edit: The delegate in NewViewController is called when all the fields in the NewViewController are filled in and is submitted. For now that's the only thing that is delegated from NewViewController. The UIViewController that shows NewViewController modally is what is set as the delegate (thus self in this case).

A:

I think this approach is fine, although you may want to do a little more error checking just in case. If this is a named segue, you can check segue.identifier to make sure this is the view controller you're expecting. I would also check the classes like so:
if ([segue.destinationViewController isKindOfClass:[UINavigationController class]])
{
    UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
    if ([navController.topViewController isKindOfClass:[NewViewController class]])
    {
        NewViewController *newViewController = (NewViewController *)navController.topViewController;
        [newViewController setDelegate:self];
    }
}

