从超级视图中删除视图,通知iPhone上的子视图
当视图从其超级视图中删除时,会触发什么事件?其子视图是否收到任何消息? 例如,我将subview2和subview3添加到subview1中,如中所示 超级视图->子视图1->子视图2->子视图3 如果我删除子视图1,例如从超级视图中删除视图,通知iPhone上的子视图,iphone,cocoa-touch,uikit,uiview,Iphone,Cocoa Touch,Uikit,Uiview,当视图从其超级视图中删除时,会触发什么事件?其子视图是否收到任何消息? 例如,我将subview2和subview3添加到subview1中,如中所示 超级视图->子视图1->子视图2->子视图3 如果我删除子视图1,例如 [subview1 removeFromSuperview]; 其子视图(子视图2和子视图3)接收什么事件 有没有办法让子视图知道它们的超级视图已被删除?我认为子视图(2,3)在子视图1本身被删除时不会收到任何事件(文档中至少没有提及) 编辑: 多想想。。。我相信当子视图
[subview1 removeFromSuperview];
其子视图(子视图2和子视图3)接收什么事件
有没有办法让子视图知道它们的超级视图已被删除?我认为子视图(2,3)在子视图1本身被删除时不会收到任何事件(文档中至少没有提及) 编辑: 多想想。。。我相信当子视图1被释放时,子视图(2,3)本身不会收到事件 但作为子视图1被释放的副作用,如果子视图1没有保留在其他地方,它的引用计数将达到0,并且将被释放。解除分配期间,子视图1将释放其所有子视图 在这种情况下,他们会被释放,我不确定这是否是你想要的
参见Jane的答案。我认为子视图(2,3)在子视图1本身被删除时不会收到任何事件(文档中至少没有提到任何内容) 编辑: 多想想。。。我相信当子视图1被释放时,子视图(2,3)本身不会收到事件 但作为子视图1被释放的副作用,如果子视图1没有保留在其他地方,它的引用计数将达到0,并且将被释放。解除分配期间,子视图1将释放其所有子视图 在这种情况下,他们会被释放,我不确定这是否是你想要的
参见Jane的答案。这取决于子视图2和子视图3的保留计数。如果通过[[UIView alloc]initWithFrame:frame]创建它们,然后将它们添加为子视图,则它们的保留计数为2。(或3,如果在保留的属性中保留引用,即self.subview2=[ 因此,如果您希望在释放子视图1时释放它们,那么请确保在将它们添加为子视图后为它们提供另一个释放,以便它们的保留计数仅为添加为子视图时的单个保留计数。类似于以下内容
UIView* subview2 = [[UIView alloc] initWithFrame:myFrame];
[subview1 addSubview:subview2];
[subview2 release];
这取决于子视图2和子视图3的保留计数。如果通过[[UIView alloc]initWithFrame:frame]创建它们,然后将它们添加为子视图,则它们的保留计数为2。(或者,如果在保留属性中保留引用,即self.subview2=[ 因此,如果您希望在释放子视图1时释放它们,那么请确保在将它们添加为子视图后为它们提供另一个释放,以便它们的保留计数仅为添加为子视图时的单个保留计数。类似于以下内容
UIView* subview2 = [[UIView alloc] initWithFrame:myFrame];
[subview1 addSubview:subview2];
[subview2 release];
从其超级视图中删除视图时,其所有子视图也将从中删除,从而将RetainOut减少1 查看以下代码snipet:
randomImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"oldbg.png"]];
randomImage.frame = CGRectMake(10, 10, 20, 20);
aview = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 200)];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);
[aview addSubview:randomImage];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);
[randomImage release];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);
[self.view addSubview:aview];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);
[aview release];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);
[aview removeFromSuperview];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);
控制台日志如下所示:
2009-08-09 23:29:42.512 ActionSheetTest[744:20b] aview retain=1,image retain=1
2009-08-09 23:29:42.513 ActionSheetTest[744:20b] aview retain=1,image retain=2
2009-08-09 23:29:42.515 ActionSheetTest[744:20b] aview retain=1,image retain=1
2009-08-09 23:29:42.516 ActionSheetTest[744:20b] aview retain=2,image retain=1
2009-08-09 23:29:42.517 ActionSheetTest[744:20b] aview retain=1,image retain=1
实际上,在最后一次NSLog时,应用程序将崩溃,因为两个对象的retainCount=0
希望这能有所帮助。当一个视图从其superview中删除时,其所有子视图也将从中删除,从而将RetainOut减少一个 查看以下代码snipet:
randomImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"oldbg.png"]];
randomImage.frame = CGRectMake(10, 10, 20, 20);
aview = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 200)];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);
[aview addSubview:randomImage];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);
[randomImage release];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);
[self.view addSubview:aview];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);
[aview release];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);
[aview removeFromSuperview];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);
控制台日志如下所示:
2009-08-09 23:29:42.512 ActionSheetTest[744:20b] aview retain=1,image retain=1
2009-08-09 23:29:42.513 ActionSheetTest[744:20b] aview retain=1,image retain=2
2009-08-09 23:29:42.515 ActionSheetTest[744:20b] aview retain=1,image retain=1
2009-08-09 23:29:42.516 ActionSheetTest[744:20b] aview retain=2,image retain=1
2009-08-09 23:29:42.517 ActionSheetTest[744:20b] aview retain=1,image retain=1
实际上,在最后一次NSLog时,应用程序将崩溃,因为两个对象的retainCount=0
希望这能有所帮助。因为这个问题仍然悬而未决,这里有一个答案:
@implementation MySubview
- (void)willMoveToSuperview:(UIView *)newSuperview {
if (!newSuperview) {
// I'm being removed from my superview.
}
}
- (void)didMoveToSuperview {
if (!self.superview) {
// I no longer have a superview.
}
}
@end
如果您需要相反的方法,下面是如何通知superview其子视图正在达到峰值
@implemenation MySuperview
- (void)willRemoveSubview:(UIView *)subview {
// I'm about to remove this view.
}
@end
其子视图(子视图2和子视图3)接收什么事件?subview1
会收到通知,但是subview2
和subview3
需要通过subview1
传递该消息(这不是自动完成的)
有没有办法让子视图知道其超级视图已被删除?您可以创建一个简单的委托协议,也可以为此扩展
UIView
@implementation UIView (superview_notification)
- (void)notifyMyChildrenAboutTheSuperviewChange {
[[self subviews] makeObjectsPerformSelector:@selector(notifyMyChildrenAboutTheSuperviewChange)];
}
@end
但是,请记住,如果您真的想知道它们何时不再出现在屏幕上(并且它们没有superview这一事实是您的次要目标),则所有子视图都将通过上述方法的UIWindow
镜像得到通知
@implementation MySubview
- (void)willMoveToWindow:(UIWindow *)newWindow {
if (!newWindow) {
// I'm being removed from the screen.
}
}
- (void)didMoveToWindow {
if (!self.window) {
// I'm offscreen.
}
}
@end
由于这个问题仍然悬而未决,这里有一个答案:
@implementation MySubview
- (void)willMoveToSuperview:(UIView *)newSuperview {
if (!newSuperview) {
// I'm being removed from my superview.
}
}
- (void)didMoveToSuperview {
if (!self.superview) {
// I no longer have a superview.
}
}
@end
如果您需要相反的方法,下面是如何通知superview其子视图正在达到峰值
@implemenation MySuperview
- (void)willRemoveSubview:(UIView *)subview {
// I'm about to remove this view.
}
@end
其子视图(子视图2和子视图3)接收什么事件?subview1
会收到通知,但是subview2
和subview3
需要通过subview1
传递该消息(这不是自动完成的)
有没有办法让子视图知道其超级视图已被删除?您可以创建一个简单的委托协议,也可以为此扩展
UIView
@implementation UIView (superview_notification)
- (void)notifyMyChildrenAboutTheSuperviewChange {
[[self subviews] makeObjectsPerformSelector:@selector(notifyMyChildrenAboutTheSuperviewChange)];
}
@end
但是,请记住,如果您真的想知道它们何时不再出现在屏幕上(并且它们没有superview这一事实是您的次要目标),则所有子视图都将通过上述方法的UIWindow
镜像得到通知
@implementation MySubview
- (void)willMoveToWindow:(UIWindow *)newWindow {
if (!newWindow) {
// I'm being removed from the screen.
}
}
- (void)didMoveToWindow {
if (!self.window) {
// I'm offscreen.
}
}
@end
removeFromSuperView的文档说,“接收器也被释放了;”。释放视图不释放其子视图吗?removeFromSuperView的文档说,“接收器也被释放了;”。发布视图是否会释放其子视图?+1,Jane-很棒的网站/公司/生活…我对你的答案很感兴趣,如图所示…Jane,正如你所提到的,我在将子视图添加到超级视图后将其释放。但是当我删除超级视图时,请通过[self removeFromSuperView]说“subview1”;//未调用将子视图1从其superview中移除子视图2的dealloc。有关“removeFromSuperView”的文档提到,调用它也将释放它。Am