2009-06-24 73 views
1

將視圖從其超級視圖中刪除時觸發了什麼事件?它的子視圖是否收到任何消息? 例如,我有subview2和subview3加入subview1如從其SuperView中刪除視圖,在iPhone上通知子視圖

super_view - > subview1 - > subview2 - > subview3

如果刪除subview1例如通過

[subview1 removeFromSuperview]; 

它的子視圖(子視圖2和子視圖3)接收到什麼事件?

有沒有辦法讓子視圖知道他們的超級視圖被刪除?

回答

0

當subview1本身被刪除(至少在文檔中沒有提到任何內容),我不認爲子視圖(2,3)會收到任何事件。

編輯

有關它的更多的思考......我相信subview1被釋放時,子視圖(2,3)將不會接收到事件本身。

但是,作爲subview1獲得釋放的副作用,如果subview1沒有保留在其他地方,它的引用計數將達到0,它將被取消分配。在取消分配期間,子視圖1將釋放其所有子視圖。

在這種情況下,他們將獲得釋放,我不知道這是你是什麼後。

查看Jane的回答。

+0

removeFromSuperView的文檔說:「接收器也被釋放;」。 不發佈視圖會發布其子視圖嗎? – MAbbas 2009-06-25 04:42:36

5

它取決於子視圖2和子視圖3的保留計數。如果通過[[UIView alloc] initWithFrame:frame]創建它們,然後將它們添加爲子視圖,則它們的保留計數爲2.(或3,如果在保留屬性中保留引用,即self.subview2 = [[...

因此,如果您希望在subview1發佈時將它們釋放,那麼請確保在將它們添加爲子視圖之後爲它們提供另一個版本,以便它們的保留計數僅僅是添加爲一個子視圖。事情是這樣的......

UIView* subview2 = [[UIView alloc] initWithFrame:myFrame]; 
[subview1 addSubview:subview2]; 
[subview2 release]; 
+0

+1,簡 - 偉大的網站/公司/生活...我很好奇,因爲你的答案和圖片... – stefanB 2009-06-25 05:30:08

0

當畫面從它的父刪除,其所有子視圖也從中從而降低retaincout由一個刪除。

請看下面一小段代碼片段:

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應用程序會崩潰,因爲兩者的OBJETS有retainCount = 0 。

希望這會有所幫助。

1

因爲這個問題仍然是開放的,這裏有一個答案:

@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 

如果您需要相反,這裏的上海華是如何通知其子視圖是peacing出來。

@implemenation MySuperview 
- (void)willRemoveSubview:(UIView *)subview { 
    // I'm about to remove this view. 
} 
@end 

它的子視圖(subview2和subview3)會收到什麼事件?
subview1已收到通知,但subview2subview3需要將該消息傳遞給subview1(這不會自動完成)。

有沒有辦法讓子視圖知道他們的超級視圖被刪除?
您可以創建一個簡單的委託協議,或者您可以爲此擴展UIView

@implementation UIView (superview_notification) 
- (void)notifyMyChildrenAboutTheSuperviewChange { 
    [[self subviews] makeObjectsPerformSelector:@selector(notifyMyChildrenAboutTheSuperviewChange)]; 
} 
@end 

請記住,但是,如果你真的想知道,當他們不再在所有的屏幕(而事實上,他們沒有一個上海華上是繼發於你的目標),所有子視圖將通過上述方法的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 
相關問題