Objective c 如何初始化CMMotionManager globaly以供不同的类使用?
我试图在一个类中初始化设备运动管理器,该类包含我在不同类中使用的所有全局变量。然而,我似乎无法让这个工作 在我的全球课程中,我有以下内容:Objective c 如何初始化CMMotionManager globaly以供不同的类使用?,objective-c,xcode,ios5,Objective C,Xcode,Ios5,我试图在一个类中初始化设备运动管理器,该类包含我在不同类中使用的所有全局变量。然而,我似乎无法让这个工作 在我的全球课程中,我有以下内容: // Motion Manager CMMotionManager *motionManager; 然后在不同的类头文件中设置属性: @property (retain) CMMotionManager *motionManager; 在.m文件中,我合成并启动更新: @synthesize motionManager; motionManager =
// Motion Manager
CMMotionManager *motionManager;
然后在不同的类头文件中设置属性:
@property (retain) CMMotionManager *motionManager;
在.m文件中,我合成并启动更新:
@synthesize motionManager;
motionManager = [[CMMotionManager alloc] init];
if (motionManager.deviceMotionAvailable)
{
motionManager.deviceMotionUpdateInterval = 1.0/100.0;
[motionManager startDeviceMotionUpdates];
NSLog(@"Device Started");
}
但是当我在第三节课上说这个的时候:
motionManager.deviceMotionAvailable
它返回否
PD:两个类都导入全局类,第三个类导入第二个类的头。好的,看来我可以使用单例类来实现这一点: h文件:
//
// MotionManagerSingleton.h
// Air Interface
//
// Created by MacBook on 11/12/28.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
// File created using Singleton XCode Template by Mugunth Kumar (http://blog.mugunthkumar.com)
// More information about this template on the post http://mk.sg/89
// Permission granted to do anything, commercial/non-commercial with this file apart from removing the line/URL above
#import <Foundation/Foundation.h>
#import <CoreMotion/CoreMotion.h>
@interface MotionManagerSingleton : NSObject
+ (MotionManagerSingleton*) sharedInstance;
- (void)startMotionManager;
- (CMAcceleration)getCurrentAcceleration;
- (CMAttitude*)getCurrentAttitude;
- (CMAttitude*)getStartingAttitude;
- (void)setStartingAttitude: (CMAttitude*) ref;
- (bool)isDeviceReady;
- (void)destroyMotionManager;
- (NSTimeInterval) getStamp;
@end
只要我想使用它,我就可以在类头中声明一个变量,如下所示:
MotionManagerSingleton *Manager;
并在m文件中这样使用它:
Manager = [MotionManagerSingleton sharedInstance];
if ([Manager isDeviceReady]) {
NSLog(@"Device Is Ready on Drawing View Controller");
}
referenceAttitude = [Manager getStartingAttitude];
好的,看来我可以使用singleton类来实现这一点: h文件:
//
// MotionManagerSingleton.h
// Air Interface
//
// Created by MacBook on 11/12/28.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
// File created using Singleton XCode Template by Mugunth Kumar (http://blog.mugunthkumar.com)
// More information about this template on the post http://mk.sg/89
// Permission granted to do anything, commercial/non-commercial with this file apart from removing the line/URL above
#import <Foundation/Foundation.h>
#import <CoreMotion/CoreMotion.h>
@interface MotionManagerSingleton : NSObject
+ (MotionManagerSingleton*) sharedInstance;
- (void)startMotionManager;
- (CMAcceleration)getCurrentAcceleration;
- (CMAttitude*)getCurrentAttitude;
- (CMAttitude*)getStartingAttitude;
- (void)setStartingAttitude: (CMAttitude*) ref;
- (bool)isDeviceReady;
- (void)destroyMotionManager;
- (NSTimeInterval) getStamp;
@end
只要我想使用它,我就可以在类头中声明一个变量,如下所示:
MotionManagerSingleton *Manager;
并在m文件中这样使用它:
Manager = [MotionManagerSingleton sharedInstance];
if ([Manager isDeviceReady]) {
NSLog(@"Device Is Ready on Drawing View Controller");
}
referenceAttitude = [Manager getStartingAttitude];
每个iOS应用程序中都有一个单例类,通常称为
AppDelegate
:-)
这是因为AppDelegate
在技术上实现了uiapplicationelegate
所以,我遵循了,它对我非常有效
我希望这会有所帮助,现在回答还不算太晚。每个iOS应用程序中都已经有一个单例类,通常称为
AppDelegate
:-)
这是因为AppDelegate
在技术上实现了uiapplicationelegate
所以,我遵循了,它对我非常有效
我希望这会有所帮助,现在回答还不算太晚。您应该思考一下为什么
CMMotionManager
没有作为单例实现,然后问问自己,用单例来包装它是正确的方法。显然,单例有优点,但也有缺点(例如,在oop方面),这就是为什么很多人认为它是一种反模式。我不明白你在暗示什么。你应该思考为什么<代码> CMMOSMODE管理器< /代码>没有实现为一个单独的,然后问问自己,这是正确的方式去包装它在一个单一的。显然,单体有优势,但它也有(例如,在OOP)方面的缺点,这就是为什么很多人认为它是反模式的。我不明白你在暗示什么。