• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
What is the best way to call parent view controller methods from within child view controller?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

What is the best way to call parent view controller methods from within child view controller?


  • Subject: What is the best way to call parent view controller methods from within child view controller?
  • From: Alex Reynolds <email@hidden>
  • Date: Sat, 19 Sep 2009 13:34:50 -0700

I have a view hierarchy managed with UINavigationController, of the form A -> B, where A and B are UIViewController subclasses.

When I am in the B view, I have a backBarButtonItem in the navigation bar that will pop the application back to the A view, when selected.

The title of the backBarButtonItem was initially set in A, just before I pushed the B view onto the navigation stack.

Assuming I am in B and I would like to change the title of the backBarButtonItem, what is the best way to accomplish this?

Currently, I have a method in A that sets the title property value:

	- (void) setBackBarButtonItemTitle:(NSString *)newTitle {
	    self.navigationItem.backBarButtonItem.title = newTitle;
	}

When in B, I call -viewControllers to get the array of view controllers, referencing the second to last element to call A's - setBackBarButtonItemTitle:

NSArray *viewControllerArray = [self.navigationController viewControllers];
int parentViewControllerIndex = [viewControllerArray count] - 2;
[[viewControllerArray objectAtIndex:parentViewControllerIndex] setBackBarButtonItemTitle:@"New Title"];


This approach is clunky but it seems to work correctly.

However, I don't know why the following does not work, when called within B:

[(A *)(self.navigationController.parentViewController) setBackBarButtonItemTitle:@"New Title"];

In fact, calling any parent view methods using the parentViewController property does not work.

Why does this second approach (using the parentViewController property) fail to work? Am I not using this property correctly?

Thanks,
Alex
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: What is the best way to call parent view controller methods from within child view controller?
      • From: Jesse Armand <email@hidden>
  • Prev by Date: Loading background wallpaper
  • Next by Date: Push-on push-off buttons doesn't show state
  • Previous by thread: Loading background wallpaper
  • Next by thread: Re: What is the best way to call parent view controller methods from within child view controller?
  • Index(es):
    • Date
    • Thread