Ashley Staggs
Ashley Staggs

Reputation: 1567

iOS - Access Modal's Parent Property

I am probably overcomplicating this but is there a way to access a property of a modal's parent?

So, I call "presentModalViewController" and then I can access some properties on the view controller that just called it from the modal.

Thanks, Ashley

Upvotes: 3

Views: 4274

Answers (3)

jrturton
jrturton

Reputation: 119242

Your answer was nearly correct - you want presentingViewController rather than parentViewController, however this could lead to tight coupling (dependency between the two classes, meaning they can only work with each other) if you're not careful.

It's best to define a protocol still, but your delegate property is not necessary - it will have the same value as the presentingViewController property.

Upvotes: 2

fengd
fengd

Reputation: 7569

if you are using iOS 5, you can call self.presentingViewController to access the parent view controller

here is apple reference

Upvotes: 4

Ashley Staggs
Ashley Staggs

Reputation: 1567

Ok, so since asking this question I have tried a few techniques which looked as though they would work but didn't.

Such an example is this.

((pController *)self.parentViewController).testString;

However, while it was a regular UIViewController that presented the modal, the parent was actually a UITabBarController, and even using selectedViewController didn't work.

My solution was to add to my modal's .h file

id delegate;

and

@property (nonatomic, assign) id delegate;

After synthesizing it in the implementation file, alloc/init the modalViewController, but just before presenting it I set

modalViewController.delegate = self;

This way I could call self.delegate from within my modal. This still wasn't enough as this doesn't say which view controller it is, so I can't say

self.delegate.testString;

But the casting I learnt earlier allowed me to get a fully working solution of

((pController *)self.delegate).testString;

I hope I haven't just babbled my way through this, and I hope that this can help someone in the future.

Upvotes: 1

Related Questions