How to set the header on the destination view controller during the preparation of ForSegue:

I just want to set the title on my destination view controller so that it appears in the navigation bar of the navigation controller in the prepareForSegue method: however, however, it set its title or navigationItem as follows:

[segue.destinationViewController setTitle:@"doesn't work"]; [segue.destinationViewController.navigationItem setTitle:@"this either"];

does not work because the view view controller of the destination view has not yet been loaded. Can I do this without creating a custom view controller?

+8
ios uistoryboard uistoryboardsegue
source share
4 answers

Try accessing your ViewController built into a UINavigationController like this. First you give your segue identifier in the interface builder, then you prepareForSegue access the prepareForSegue method prepareForSegue and set the header by accessing the topViewController property of the navigation controller, you go to topViewController .

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"yourSegueIdentifier"]) { UINavigationController *navController = (UINavigationController*)[segue destinationViewController]; YourViewController *destViewController = (YourViewController* )[navController topViewController]; destViewController.navgationItem.title.text = @"Your new title"; } } 
+11
source share

If you want the static name to be possible now (that is, in Xcode 4.6.3), this can be done on the Storyboard by simply setting the title in the navigation bar on the corresponding view controller.

If, however, you want the title of the navigation bar to change according to what is being viewed, for example, with a certain row of a particular row of the table, as far as I know, this should be set programmatically.

It took FOREVER (beginner, sigh!) To figure out how to change Julian Vogels' correct answer in order to trigger the key that I wanted to use. Here's what worked:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"DetailSegue"]) { // Fetch Item NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; NSDictionary *item = [self.groceries objectAtIndex:[indexPath row]]; // Configure Detail View Controller TPDetailViewController *vc = [segue destinationViewController]; vc.navigationItem.title = [item objectForKey:@"name"]; [vc setItem:item]; } 
+1
source share

This is for segue in another UITableView

  • Set the public NSString property in the destination UITableViewController file.

    @property (strong, nonatomic) NSString *navBarTitle;

  • To override the installer in the .m file.

    - (void) setNavBarTitle:(NSString *)navBarTitle { _navBarTitle = navBarTitle; }

  • In the original tableView segue method, skip any row you need in the header.

    - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString: @"userDetails"]) { UsersActivitiesTableViewController *destinationController = segue.destinationViewController; //Get the index path of the cell the user just clicked NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; //Grab the object from the array of users if the next view requires a specific title for a user name etc. UserMO *thisUser = [self.users objectAtIndex:indexPath.row]; //Pass the string you want as the title to the public NSString property destinationController.navBarTitle = thisUser.name; } }

  • And now for the important bit .... In the destination controller, get the top view controller and set the title property, after loading the view and before displaying it:

    - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController topViewController].title = self.navBarTitle; }

+1
source share

If segue is a push seg, which should be if you use a UINavigationController, then the destination view controller is automatically added to the window hierarchy, and you don't even need to identify the UINavigationController:

 if ([segue.identifier isEqualToString:@"yourSegueNameHere"]) { [segue.destinationViewController setTitle:@"yourTitleHere"]; } 
0
source share

All Articles