Question

I'm currently writing an app with multiple users. I would like to use the same "Profile" view in storyboard to display info for all the users.

The plan was to set the title of the "Profile" view and then push it.

 - (void) pushGeneralProfileViewFrom:(UIViewController *)target usernameAsTitleOfView:(NSString *)title {

    UIViewController *myView = [target.storyboard instantiateViewControllerWithIdentifier:@"GeneralProfileView"]; // created in storyboard, Feed View
    myView.title = title;
    [target.navigationController pushViewController:myView animated:YES];
}

This worked great. However, the "profile" view also has buttons that lead it to other views in the storyboard. Those views also display specific info for the relevant user.

I was planning to use the name of the backBarButtonItem to know the user I need to display the info for. While I can see the back button with the username in the simulator, I'm unable to get it's self.navigationController.navigationItem.backBarButtonItem.title or self.navigationItem.backBarButtonItem.title. Both return null.

What am I doing wrong? Also, I have a feeling this is not the best practice to handle those kind of things. I've searched the web and so far haven't seen a better way to pass values to a view I'm pushing. Suggestions?

Thank you.

Was it helpful?

Solution

The backBarButtonItem that you see when your view controller is visible in the navigation controller interface is not the backBarButtonItem of this view controller. It is the backBarButtonItem of the second view controller, the one behind this one in the stack.

So what you want is this:

UIInteger c = [self.navigationController.viewControllers count];
UIViewController* vc2 = [self.navigationController.viewControllers objectAtIndex:c-2];

Now get the backBarButtonItem of the navigationItem of that view controller. Or just get its title if they are the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top