问题描述
我有基于视图的应用程序,并且我希望在从子视图中删除子视图时调用superview(parentview)中的方法。
1)两个(父,子)视图都是UIViewController的子类
2)将子视图添加为[self.view addSubview:subviewController];
3)我通过[self.view removeFromSuperview]删除父母的子视图;
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];
任何人都可以帮助我。
推荐答案
你似乎对术语有点困惑。 UIViews和UIViewControllers是分开的东西。视图控制器有视图。视图可以知道何时从中删除了某些内容,但在这种情况下,您希望视图控制器知道何时从视图中删除某些内容?
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?
不做任何其他假设关于程序中的类层次结构,我能想到的最好的方法是创建一个自定义子类UIView,它将视图控制器保持为委托,并在要删除某些内容时通知它。使用其中一个作为父视图,并将父视图控制器作为委托给它。
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.
示例UIView子类,接口:
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 UIViewThatNotifiesViewController
@synthesize delegate;
- (void)willRemoveSubview:(UIView *)subview
{
[delegate view:self willRemoveSubview:subview];
[super willRemoveSubview:subview];
}
@end
假设您的父视图控制器当前视图是UIView类型,将其更改为(在Interface Builder和Xcode中)为UIViewThatNotifiesViewController类型。声明您的视图控制器实现UIViewThatNotifiesViewController协议以避免编译器警告。然后将这样的内容添加到视图控制器:
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");
}
}
风格上更正常的方式做这种事情是让每个视图控制器管理整个屏幕充满信息。因此,您不要将视图从一个添加到另一个。相反,你使用presentModalViewController:将控制从一个控制器传递到另一个控制器并解除传递给它:传递回来(通常使用[self.parentViewController dismissModalViewController:...],这样孩子们可以解雇自己而不管父母)。然后,您可以使用视图控制器方法viewWillAppear,viewDidAppear,viewWillDisappear和viewWillAppear来确定您是否即将从可见转换为不可见,反之亦然。
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.
这篇关于iPhone如何从子视图中调用父视图中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!