2

I have 2 viewcontrollers A, B

  1. ViewController A is being displayed on the screen
  2. The view of ViewController B is added to the view of ViewController A.

How can i access ViewController A from ViewController B without passing a pointer?

self.parentViewController does not work it returns nuil

aryaxt
  • 76,198
  • 92
  • 293
  • 442

2 Answers2

1

Why is the view of viewController B added to the view of viewController A? If you are meaning to add a view to viewController A, you can do that by allocating a new UIView and displaying that on top of the current view.

On the other hand if you are trying to transition from one view controller to another, i.e going from A to B and then back to A, you should use a UINavigationController as specified in the View Controller Programming Guide in the Apple Documentation.

Source: View Controller Programming Guide

EDIT: If you want to use [[self parentViewController] yourMethodHere]; you will need a view hierarchy, for which you can use the UINavigationController. In your current scheme there is no hierarchy built so there is no parent view controller.

aqua
  • 3,269
  • 28
  • 41
0

This should work though its not very elegant:

UIViewController* viewControllerA = 
    (UIViewController*) [[self.view superview] nextResponder];
Felix
  • 35,354
  • 13
  • 96
  • 143
  • this returns a UINavigaionView, it doesn't return he view controller. I ended up passing the parent to child manually – aryaxt Jan 13 '11 at 22:49