开发者

iPhone how to call a method in parentview from its subview

开发者 https://www.devze.com 2023-02-12 09:57 出处:网络
i have view based application, and i want to call a method in superview(parentview) when its sub view is removed from it. 开发者_运维知识库

i have view based application, and i want to call a method in superview(parentview) when its sub view is removed from it. 开发者_运维知识库 1) both(parent, sub) view are subclass of UIViewController 2) added subview as [self.view addSubview:subviewController]; 3) i removing subview from parent by [self.view removeFromSuperview];

can any one please help me.


You seem to be a bit confused on terminology. UIViews and UIViewControllers are separate things. View controllers have views. A view can know when something is removed from it, but in this case you want a view controller to know when something is removed from its view?

Without making any other assumptions about the class hierarchy in your program, the best I can come up with is to create a custom subclass UIView that keeps a view controller as a delegate and notifies it when something is to be removed. Use one of those as the parent view and give it the parent view controller as a delegate.

When the parent view controller gets the message, it compares the view that will be removed to the one belonging to the child view controller. If they match then you've got what you wanted.

Example UIView subclass, interface:

@protocol UIViewThatNotifiesViewControllerDelegate
- (void)view:(UIView *)view willRemoveSubview:(UIView *)subview;
@end

@interface UIViewThatNotifiesViewController: UIView
{
    UIViewController <UIViewThatNotifiesViewControllerDelegate> *delegate;
}

@property (nonatomic, assign) UIViewController <UIViewThatNotifiesViewControllerDelegate> *delegate;
@end

Implementation:

@implementation UIViewThatNotifiesViewController

@synthesize delegate;

- (void)willRemoveSubview:(UIView *)subview
{
    [delegate view:self willRemoveSubview:subview];
    [super willRemoveSubview:subview];
}

@end

Assuming your parent view controller's current view is of type UIView, change it (in Interface Builder and in Xcode) to be of type UIViewThatNotifiesViewController. Declare that your view controller implements the UIViewThatNotifiesViewController protocol to avoid compiler warnings. Then add something like this to the view controller:

- (void)view:(UIView *)view willRemoveSubview:(UIView *)subview
{
    if(subview == subviewController.view)
    {
        NSLog(@"his view is in the process of being removed");
    }
}

The stylistically more normal way to do this sort of thing is to have each view controller manage an entire screen full of information. So you don't add the view from one to another. Instead you use presentModalViewController: to pass control from one controller to another and dismissModalViewController: to pass it back (often with [self.parentViewController dismissModalViewController:...] so that children can dismiss themselves regardless of the parent). You can then use the view controller methods viewWillAppear, viewDidAppear, viewWillDisappear and viewWillAppear to determine whether you're about to transition from visible to invisible or vice versa.

0

精彩评论

暂无评论...
验证码 换一张
取 消