释放视图控制器并删除视图

时间:2011-06-03 16:00:33

标签: iphone objective-c cocoa-touch ipad

快速提问:

如果我使用

[someViewController.view addSubView:otherViewController.view];

添加视图。然后使用

[otherViewController.view removeFromSuperView] 

要删除该视图,当我致电[otherViewController release]

时,我的应用会崩溃

崩溃点位于[super dealloc]类实施的dealloc方法中的otherViewControll行。

otherViewController是对视图控制器的引用。我的视图已被removeFromSuperViewed后调用release。当我调用release时,它是一个有效的指针。

我在这里做错了什么?

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];

}

我尝试使用Button来调用 - (IBAction)showNextRecod,它不会崩溃!! 只有当我使用手势调用相同的方法时,它才会崩溃

此致

利奥

3 个答案:

答案 0 :(得分:3)

当您将视图控制器的视图作为子视图添加到另一个视图时,您只保留其在内存中的视图而不是控制器本身。因此,必须将视图控制器保留在其他位置(最有可能使其成为属性)

这有意义吗?

答案 1 :(得分:1)

在您的手势选择器showNextRecod:中 您应该删除目标[pGestureRecognizer removeTarget:nil action:NULL];

答案 2 :(得分:1)

@leo 你只是在someViewController.view中添加了otherViewController.view。

只是添加视图而不是分配它,然后只是从视图中删除它。

我不明白为什么你在使用 [otherViewController发布] 当你没有为它分配相同的视图时。

您肯定可以记录otherViewController.view计数。查看您获得的视图的保留计数。