繁体   English   中英

发布视图 controller 和移除视图

[英]release view controller and removing view

快速提问:

如果我使用

[someViewController.view addSubView:otherViewController.view];

添加视图。 然后使用

[otherViewController.view removeFromSuperView] 

要删除视图,当我调用[otherViewController release]时,我的应用程序将崩溃

崩溃点在我的otherViewControll class 实现的dealloc方法中的[super dealloc]行中。

otherViewController 是对视图 controller 的引用。 在它的视图被 removeFromSuperView'ed 后,我调用 release。 当我调用 release 时,它是一个有效的指针。

我在这里做错了什么?

otherViewController 的 dealloc class 实现

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

}

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

问候

狮子座

当您将视图控制器的视图作为子视图添加到另一个视图时,您只会将其视图保留在 memory 中,而不是 controller 本身。 因此,您必须在其他地方保留视图 controller (很可能使其成为属性)

那有意义吗?

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

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

只需添加视图而不是分配它,然后将其从您的视图中删除。

当您没有分配相同的视图时,我不明白为什么要使用[otherViewController release]

当然,您可以记录 otherViewController.view 计数。 查看您获得的该视图的保留计数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM