Re: What is the best way to call parent view controller methods from within child view controller?
Re: What is the best way to call parent view controller methods from within child view controller?
- Subject: Re: What is the best way to call parent view controller methods from within child view controller?
- From: Jesse Armand <email@hidden>
- Date: Sun, 20 Sep 2009 18:37:56 +0700
You can create an ivar in class B that refer to class A instance. Set
that ivar, when class B instance is created.
So, you can call the methods in class A by using that instance,
without searching through the view controllers array.
Besides, you can also change the title before class B is pushed.
You should not refer to the navigation controller's parent.
You should refer to the current view controller's parent.
self.parentViewController.navigationItem.title = @"New Title"
That's if the parentViewController is not nil.
I never used this property.
Jesse Armand
----------------------------------------
(http://jessearmand.com)
On Sun, Sep 20, 2009 at 3:34 AM, Alex Reynolds <email@hidden> wrote:
> 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
>
_______________________________________________
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