2011-06-03 85 views
0

快速的問題:如果我使用釋放視圖控制器和刪除視圖

[someViewController.view addSubView:otherViewController.view]; 

添加視圖

。然後用

[otherViewController.view removeFromSuperView] 

刪除視圖,當我打電話[otherViewController release]

的碰撞點在[super dealloc]線在我的otherViewControll類實現dealloc方法,我的應用程序會崩潰。

otherViewController是對視圖控制器的引用。我認爲它的視圖已被removeFromSuperView'ed釋放。在我發佈的時候,這是一個有效的指針。

我在這裏做錯了什麼?

otherViewController的dealloc的類實現

- (void)dealloc { 
    [popVC release]; 
    [photoContainer release]; 
    [photoView release]; 
    [recordName release]; 
    [recordIngr release]; 
    [recordDesc release]; 
    [recordPrice release]; 
    [quantity release]; 
    [pricingLabel release]; 
    [increaseButton release]; 
    [decreaseButton release]; 

    [pricingTableVC release]; 
    [pricingTable release]; 

    [super dealloc]; // <--- crash point 
} 

更新:呼叫跟蹤

2011-06-04 00:35:05.110 MyApp[2308:207] -[__NSCFType _viewDelegate]: unrecognized selector sent to instance 0x4b6feb0 
2011-06-04 00:35:05.124 MyApp[2308:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType _viewDelegate]: unrecognized selector sent to instance 0x4b6feb0' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x00dd75a9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x00f2b313 objc_exception_throw + 44 
    2 CoreFoundation      0x00dd90bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
    3 CoreFoundation      0x00d48966 ___forwarding___ + 966 
    4 CoreFoundation      0x00d48522 _CF_forwarding_prep_0 + 50 
    5 UIKit        0x00379051 -[UIViewController dealloc] + 128 
    6 MyApp        0x00009b26 -[RecordDetailViewController dealloc] + 797 
    7 MyApp        0x00004744 __-[RecordRootViewController bringUpNextRecordDetail:isNext:]_block_invoke_2 + 77 
    8 UIKit        0x002f7fb9 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 294 
    9 UIKit        0x002f7e4b -[UIViewAnimationState animationDidStop:finished:] + 77 
    10 QuartzCore       0x01d7b99b _ZL23run_animation_callbacksdPv + 278 
    11 QuartzCore       0x01d20651 _ZN2CAL14timer_callbackEP16__CFRunLoopTimerPv + 157 
    12 CoreFoundation      0x00db88c3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19 
    13 CoreFoundation      0x00db9e74 __CFRunLoopDoTimer + 1220 
    14 CoreFoundation      0x00d162c9 __CFRunLoopRun + 1817 
    15 CoreFoundation      0x00d15840 CFRunLoopRunSpecific + 208 
    16 CoreFoundation      0x00d15761 CFRunLoopRunInMode + 97 
    17 GraphicsServices     0x0172e1c4 GSEventRunModal + 217 
    18 GraphicsServices     0x0172e289 GSEventRun + 115 
    19 UIKit        0x002d5c93 UIApplicationMain + 1160 
    20 MyApp        0x0000200c main + 102 
    21 MyApp        0x00001f9d start + 53 
) 
terminate called after throwing an instance of 'NSException' 

更新: 在-viewDidLoad,我有一個手勢識別:

{ 
    UISwipeGestureRecognizer *leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(showNextRecod:)]; 
    [leftSwipeGestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; 
    [self.view addGestureRecognizer:leftSwipeGestureRecognizer]; 
    [leftSwipeGestureRecognizer release]; 

} 

我試着使用B撥打電話 - (IBAction)showNextRecod,它不會崩潰!只有當我用手勢來調用相同的方法,它就會崩潰

問候

利奧

+0

你是如何實施你的dealloc的情況下,Brandon的回答不會幫助你? – 2011-06-03 16:11:11

+0

我剛剛用dealloc方法更新了我的帖子。 [super dealloc]之前的所有行都釋放實例變量,其中一些是我在IB中創建的UIButton。 – leo 2011-06-03 16:15:46

+0

我想知道是否調用視圖控制器視圖的removeFromSuperView會導致釋放視圖控制器崩潰 – leo 2011-06-03 16:17:42

回答

3

當您添加視圖控制器的視圖作爲一個子視圖到另一個視圖,你只保留它的視圖在內存中而不是控制器本身。因此,您必須必須保留視圖控制器在其他地方(最有可能使其屬性)

這是否有道理?

+0

otherViewController是對視圖控制器的引用。我認爲它的視圖已被removeFromSuperView'ed釋放。在我發佈的時候,這是一個有效的指針。 – leo 2011-06-03 16:12:23

+0

你在哪裏初始化otherViewController? – brandontreb 2011-06-03 16:21:29

+0

請參閱我在OP中的更新。你提醒我,我已經在viewDidLoad中添加了手勢識別器,那會是問題嗎?我可能會做錯... – leo 2011-06-03 16:45:19

1

在你的手勢選擇showNextRecod:
你應該刪除你的目標[pGestureRecognizer removeTarget:nil action:NULL];

1

@leo 你只是在someViewController.view加入otherViewController.view。

只需添加視圖不分配它,然後只是從您的視圖中刪除它。

我不是讓你爲什麼用 [otherViewController發行] 當你不分配它同樣的觀點。

確定您可以記錄otherViewController.view計數。看看你獲得的觀點有多少保留計數。

相關問題