Iphone 使用加速计退出视图时发生碰撞

Iphone 使用加速计退出视图时发生碰撞,iphone,objective-c,ios,xcode,ipad,Iphone,Objective C,Ios,Xcode,Ipad,在我的iOS应用程序中,我有一个使用加速计移动UIImageView的视图。当我退出视图回到主屏幕时,应用程序崩溃。我不明白为什么。在viewDidUnload方法中,我有以下内容: [ball removeFromSuperview]; 在.h文件中: @interface PlayShotguniPad : UIViewController <UIAccelerometerDelegate> { // Declare IBOutlets for the moving fly

在我的iOS应用程序中,我有一个使用加速计移动UIImageView的视图。当我退出视图回到主屏幕时,应用程序崩溃。我不明白为什么。在viewDidUnload方法中,我有以下内容:

[ball removeFromSuperview];
在.h文件中:

@interface PlayShotguniPad : UIViewController  <UIAccelerometerDelegate> {

// Declare IBOutlets for the moving fly
IBOutlet UIImageView *ball;

// Floats for the movement data
float valueX;
float valueY;

}

有什么我必须做的,比如在离开视图时释放任何东西,以防止崩溃吗?

您必须在dealloc中将加速度计委托设置为nil,否则将调用不再存在的视图控制器的选择器

- (void)dealloc {
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
}
[self awakeFromNib];
- (void)dealloc {
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
}