John C. John C. - 4 months ago 16
iOS Question

Set Title with Modal View doesn´t work

I initiate the following Modal Controller:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

UIViewController *modal = [storyboard instantiateViewControllerWithIdentifier:@"modalController"];
modal.title = @"Example Title";
[self presentModalViewController:modal animated:YES];


I set the title with:

modal.title = @"Example Title";


but this doesn´t work, can anyone help me?

Edit:
I have wrapped my ModalView with a UINavigationController like this

enter image description here

Answer

You are going about this in a slightly convoluted way... but to stay with your paradigm, you need to present the navigationController, not the contained viewController: trying to do it the latter way will instantiate the viewController, but this action will not pull the containing navController along with it out of the storyboard. You are setting the viewController's title property ok, but you have no (automated) way to display the title. Whereas if you instantiate the navController, it's contained viewController does get unarchived along with it as it's topViewController.

        //give the navigation controller a storyboard id eg "navVC"
    UINavigationController* modalNav = [self.storyboard instantiateViewControllerWithIdentifier:@"navVC"];
    [modalNav topViewController].title = @"Example Title";

        //[self presentModalViewController:modalNav animated:YES];
        //deprecated method, use this instead:
    [self presentViewController:modalNavController 
                       animated:YES
                    completion:nil];