ViewControllerを加速度計とGPSの両方のデリゲートとして機能させる場合は、ヘッダーファイルに両方のデリゲートプロトコルに従うことを記述します。
@interface ViewController : UIViewController <CLLocationManagerDelegate, UIAccelerometerDelegate> {
CLLocationManager *myLocationManager; // an instance variable
}
// ... your definitions
@end
次に、ViewControllerのどこかに
[UIAccelerometer sharedAccelerometer].delegate = self;
myLocationManager = [[CLLocationManager alloc] init];
myLocationManager.delegate = self;
次に、ViewControllerの別の場所に、両方のプロトコルのデリゲートメソッドを配置します
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
// ... your code
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
// ... your code
}
タイプミスがあるかもしれませんが、私はこれをコンパイルしていません。さらに、私の経験では、ロケーションマネージャーのコードはかなり大きくなる傾向があります。ViewControllerによってインスタンス化されるように、それを独自のクラスに配置する方がはるかに良い場合があります。
UIAccelerometerDelegateプロトコルの唯一のメソッドが廃止された理由を誰かが説明できますか?