我搜索了这个,但找不到任何有用的答案..如何从子类成功访问委托方法?
像这样:
#import <MapKit/MapKit.h>
@protocol customMapViewDelegate <MKMapViewDelegate>
//
@end
@interface customSubClass : MKMapView <MKMapViewDelegate>
//
@end
基本上,我想在我的子类中访问MkMapView委托,而不是在我正在实现customSubclass
的文件中。
答案 0 :(得分:0)
将其添加到您的MKMapView.h
文件中:
@protocol MKMapViewDelegate <NSObject>
@required
// Put your required delegate methods here
-(void)myFirstRequiredMethod;
@optional
// Pute your optional delegate methods here
@end
然后您可以使用customSubClass.m
文件中的委托方法:
@interface customSubClass : MKMapView <MKMapViewDelegate>
-(void)myFirstRequiredMethod
{
NSLog(@"Hello");
}
@end
c.f。此链接可获取更多信息:http://www.idev101.com/code/Objective-C/delegate.html
编辑(在OP发表评论后)
有时,您不希望将self
设置为对象的委托。您可以将其他对象完美地设置为委托。
@interface customDelegate : MyCustomDelegateClass <MKMapViewDelegate>
-(void)myDelegateMethod
{
NSLog("hello")
}
@end
然后,您的customSubClass
应该具有customDelegate
customSubClass.h
:
@interface customSubClass : MKMapView <MKMapViewDelegate>
{
}
@property (nonatomic, weak) MyCustomDelegateClass *myCustomDelegate;
@property (nonatomic, strong) MKMapView *myMKMapView;
@end
customSubClass.m
:
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.myMKMapView.delegate = self.myCustomDelegate
}
}