1
我與MKMap玩弄一些自定義的委託它不是在這裏工作,我真的不知道爲什麼錯誤:/IOS - respondsToSelector總是與自定義委託
這裏是我的代碼:
LocationViewController。^h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@protocol LocationViewDelegate <NSObject>
@required
- (void)didReceiveLocation:(CLLocation *)location;
@end
@interface LocationViewController : UIViewController <CLLocationManagerDelegate>
@property (strong, nonatomic) IBOutlet UILabel *locationLabel;
@property (nonatomic, strong) id<LocationViewDelegate> delegate;
- (IBAction)sendLocation:(id)sender;
@end
LocationViewController.m
[...]
- (IBAction)sendLocation:(id)sender {
if ([_delegate respondsToSelector:@selector(didReceiveLocation:)]) {
[_delegate didReceiveLocation:_currentLocation];
} else {
NSLog(@"nope");
}
}
[...]
MapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "LocationViewController.h"
@interface MapViewController : UIViewController <LocationViewDelegate>
@end
MapViewController.m
[...]
- (void)didReceiveLocation:(CLLocation *)location {
_mapView.centerCoordinate = location.coordinate;
}
[...]
我總是得到 「沒有」 的消息意味着respondsToSelector
返回NO。 前幾天我做了相同的例子,一切都很好。
有人可以看到問題在哪裏?
1.爲了驗證,您是否首先將您的MapViewController設置爲LocationViewController的代理?即在您的MapViewController.m中:self.locationViewController.delegate = self; 2.您正在以_delegate的身份訪問委託屬性。驗證你是在LocationViewController.m中綜合它的。就像旁邊一樣,我假定你的MapViewController擁有一個LocationViewController實例,如果是這樣的話,最好將LocationViewController中的delegate屬性設置爲'weak'以避免循環引用。 @property(nonatomic,weak)id委託; –
thomh
其實,在我做的最後一個練習中,沒有必要像self.locationViewController.delegate = self那樣做smgt。它沒有這個工作。但我試圖在這個例子中添加這行代碼,但仍然無法工作。而且,一切都很好合成 – Yaman
好吧,我發現問題來自哪裏。你說的是正確的locationViewController.delegate的東西。由於我的兩個控制器是TabBarController中的兩個不同的控制器,我不得不在AppDelegate中添加locationController.delegate = mapController – Yaman