aryaxt
aryaxt

Reputation: 77596

iPhone - Accessing the parent viewcontroller?

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

Upvotes: 2

Views: 3381

Answers (2)

aqua
aqua

Reputation: 3375

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.

Upvotes: 1

Felix
Felix

Reputation: 35384

This should work though its not very elegant:

UIViewController* viewControllerA = 
    (UIViewController*) [[self.view superview] nextResponder];

Upvotes: 0

Related Questions