presentModalViewController from UIView

2019-05-21 14:21发布

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!

3条回答
ゆ 、 Hurt°
2楼-- · 2019-05-21 15:11

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.

查看更多
在下西门庆
3楼-- · 2019-05-21 15:13

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.

查看更多
该账号已被封号
4楼-- · 2019-05-21 15:21

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?

查看更多
登录 后发表回答