如何将视图控制器设置为两个事物的委托(例如LocationManager和Accelerometer)?

And*_*son 3 iphone cocoa-touch accelerometer core-location

如果我的问题标题看起来根本不知情,我很抱歉.让我解释一下我想做什么.

我定义了以下UIViewController子类,它触发了LocationManager,并有一个开始录制按钮来保存GPS轨道.

现在我想启动加速度计并允许用户记录它.

我的ViewController子类是LocationManager委托,那么我应该为Accelerometer委托使用什么?我可以使用相同的视图,还是需要定义子视图?

这是我的UIViewController子类的接口:

@interface RootViewController : UIViewController <CLLocationManagerDelegate> {
    NSMutableArray *eventsArray;
    NSManagedObjectContext *managedObjectContext;
    CLLocationManager *locationManager;
    BOOL recording;
    UILabel *pointLabel;
    UIButton *startStop;
}

-(void)toggleButton;
Run Code Online (Sandbox Code Playgroud)

如果需要,我可以发布更多代码,但我认为这一切都适用.感谢您的帮助,我刚刚进入iPhone开发阶段,如果有的话,我的专业知识在于无指针编程语言:)

e.J*_*mes 8

将一个控制器对象作为多个事物的委托是完全合理的.如果你的LocationManager和你的Accelerometer碰巧有重叠的委托方法,即两者都要求他们的代表响应具有相同签名的方法,那么唯一的问题就是.

除此之外,您只需将控制器设置为两者的委托,就像您将其设置为委托一样:

@interface Controller : UIViewController
    <CLLocationManagerDelegate, AccelerometerDelegate>
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

然后:

[myLocationManager setDelegate:myController];
[myAccelerometer setDelegate:myController];
Run Code Online (Sandbox Code Playgroud)

请原谅命名.我不知道你需要的Accelerometer和LocationManager类的名称是什么.我只是想用任何描述性的名字想到:)