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 <alexpreyno...@gmail.com> 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 (Cocoa-dev@lists.apple.com)
>
> 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:
> http://lists.apple.com/mailman/options/cocoa-dev/mnemonic.fx%40gmail.com
>
> This email sent to mnemonic...@gmail.com
>
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to