2009-07-09 23 views
3

對不起,如果我的問題標題似乎從根本上不了解。讓我解釋我正在嘗試做什麼。如何設置視圖控制器作爲兩件事情的代表(如LocationManager和Accelerometer)?

我已經定義了以下UIViewController子類,它啓動了LocationManager,並有一個開始錄製按鈕來保存GPS軌道。

現在我想要啓動加速計並允許用戶記錄。

我的ViewController子類是LocationManager委託,那麼我應該使用哪個加速度計委託?我可以使用相同的視圖,還是需要定義子視圖?

這裏是我的UIViewController子類的接口:

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

-(void)toggleButton; 

我可以張貼如果需要更多的代碼,但我認爲這是所有應用。感謝您的幫助,我正在進行iPhone開發,並且我的專業知識(如果有的話)位於無指針編程語言:)

回答

8

將一個控制器對象作爲更多代理是完全合理的比一件事。如果你的LocationManager和加速度計碰巧有重疊的委託方法,也就是說如果它們都需要它們的委託對具有相同簽名的方法作出響應,唯一的問題就是。

除此之外,你只需設置你的控制器是代表兩個,多你將它設置成是一個委託以同樣的方式:

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

,後來:

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

請原諒命名。我不知道我頭頂的名稱是什麼,用於您需要的Accelerometer和LocationManager類。我只是使用了任何描述性的名字:)

相關問題