views:

678

answers:

3

Hi Everyone:

I am attempting to call presentModalViewController on a UIViewController in order to bring up a Address Book "view", however I am running into a bit of a problem. The UIViewController that "controls" the app simply draws a view to the screen and then lets the view do all of the handling of user interaction, etc. But now, I need to somehow need to back track to the UIViewController in order to call presentModalViewController on it. Would anyone have any ideas of how to accomplish this?

Thanks for any help!

A: 

Assuming your controller's UIView is subclassed, you could add a property to it.

@interface MyView : UIView
{
    UIViewController *parentController;
}

// Don't use retain or you'll have a circular reference
@property(nonatomic, assign) UIViewController *parentController;

@end

Then in your UIViewController code assign self to the parentController property.

-(void)viewDidLoad
{
    myView.parentController = self;
}

Is this what you were after? Begs the question though, why isn't your view controller not controlling your view?

Nick Bedford
A: 

I would suggest giving your custom view class some kind of delegate that will allow it to ask the delegate for some address book info. The delegate in this case would be the view controller which would respond by showing the picker and then returning the result of that.

Mike Abdullah
Hi Mike: I think I understand what you're saying, but I'm not sure about setting "some kind of delegate"... What is a way to accomplish this?
PF1
Well just take a look at how many of UIKit's view classes are implemented. They have a delegate property. The view can then query then message the delegate for help whenever it sees fit. e.g. you probably use a UITableViewDelegate somewhere to customise its behaviour. Understanding the delegate pattern is CRUCIAL to Cocoa/CocoaTouch.
Mike Abdullah
A: 

Your UIView should emit an event (by using the pattern used in UIButtons, etc) to a delegate (either conforming to a protocol, or using a specific selector).

If this isn't possible, view.nextResponder should be your UIViewController, if the view was created by the UIViewController.

Jason