views:

751

answers:

1

I'm sorry if my question title seems fundamentally uninformed. Let me explain what I am trying to do.

I have defined the following UIViewController subclass, which fires up LocationManager, and has a Start Recording button to save a GPS track.

Now I would like to also fire up the accelerometer and allow the user to record that as well.

My ViewController subclass is the LocationManager delegate, so what should I use for the Accelerometer delegate? Can I use the same View, or do I need to define a subview?

Here is the interface for my UIViewController subclass:

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

-(void)toggleButton;

I can post more of the code if needed, but I think this is all that applies. Thanks for your help, I'm just getting into iPhone development, and my expertise, if I have any, lies in pointer-less programming languages :)

+5  A: 

It is perfectly reasonable to have one controller object be the delegate for more than one thing. The only kicker would be if both your LocationManager and your Accelerometer happen to have delegate methods that overlap, i.e. if both of them require their delegates to respond to a method with the same signature.

Beyond that, you would simply set up your controller to be delegate for both, much the same way you set it up to be delegate for one:

@interface Controller : UIViewController
    <CLLocationManagerDelegate, AccelerometerDelegate>
{
    ...
}

And, later:

[myLocationManager setDelegate:myController];
[myAccelerometer setDelegate:myController];

Please excuse the naming. I don't know off the top of my head what the names are for the Accelerometer and LocationManager classes that you need. I just used whatever descriptive names came to mind :)

e.James