我不是Objective-C的高手,我在Javascript中創建和綁定一個簡單的東西時遇到了一些問題......只是不知道如何在Objective-C中做到這一點。UISlider委託/綁定到另一個對象?
所以我有一個UISlider。每當用戶開始拖動時,我想從一個對象(在這種情況下是一個MKAnnotation,但任何NSObject,控制器等都需要能夠偵聽)中觸發X函數。
代表是否正確使用此代理?我嘗試過沒有成功:。
以我ViewController.h
@protocol TimeSliderDelegate <NSObject>
- (void)sliderValueChanged:sender;
@end
@interface ViewController : UIViewController<MKMapViewDelegate>{
IBOutlet UISlider *timeSlider;
IBOutlet MKMapView *mapView;
id <TimeSliderDelegate> delegate;
}
@property (retain) id delegate;
然後,在我的ViewController.m,我結合我的valueChanged從我UISlider到這樣([自委託]和[發送方委託]無果嘗試。 。):
- (IBAction)timeSliderValueChanged:(id)sender {
[delegate sliderValueChanged:sender];
}
它崩潰這裏,說找不到選擇sliderValueChanged的委託實例 ...我還是會闡述我的代碼的其餘部分。
我的測試對象是我在的MKMapView加我的視圖控制器內的MKAnnotation,沒有什麼花哨這裏我只是將它加入這個樣子。
Annotation *annotation = [[Annotation alloc] init];
CLLocationDegrees lat = 0;
CLLocationDegrees lng = 0;
[(Annotation*) annotation setCoordinate:CLLocationCoordinate2DMake(lat, lng)];
[mapView addAnnotation:annotation];
然後在我的Annotation.h,導入我的viewController,並將其設置爲TimeSliderDelegate和實施sliderValueChanged。
#import "ViewController.h"
@protocol TimeSliderDelegate;
@interface Annotation : MKAnnotation<TimeSliderDelegate>
- (void)sliderValueChanged:(id)sender;
最後,這是我在Annotation.m中的代碼。從未開除。
- (void)sliderValueChanged:(id)sender{
NSLog(@"state changed");
}
仍試圖找出如何讓它在我的Annotation中觸發sliderValueChanged,但不完全確定如果Im在軌道上。
如果有人更有經驗可以帶領我,那將不勝感激:)。
你解決了嗎?如果有幫助,請接受答案。 – calimarkus 2012-05-08 11:30:03