从 NIB 加载/创建 GeoLocation 视图 - IOS 和 MapKit
我有两个类 - MapView 和 MapController。主要思想是使View成为MVC模式中的共享对象。
- MapView 具有位于 MapNib.xib 中的 MKMapView 的 IBOutlet“mapView”
- MapController 包含引用类 MapView 的属性“mapView”
然后我尝试从控制器设置 MKMapView 的一些属性,但无论如何它都不会对此做出反应。例如我尝试在 MapController :
self.mapView.mapView.showUserLocation = YES; // does not show blue point ...
MapView Class :
@interface MapView : UIViewController
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
...
//UINib *view = [UINib nibWithNibName:@"MapView" bundle:nil];
//[view instantiateWithOwner:self options:nil];
@end
MapController Class :
@interface MapController : UIViewController<
CLLocationManagerDelegate,
MKMapViewDelegate>
@property (nonatomic, retain) MapModel *mapModel;
@property (nonatomic, retain) MapView *mapView;
...
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView = [[[MapView alloc] init] autorelease];
self.mapView.mapView.showsUserLocation = YES;
} @end
我试图使 MapView 成为 UIView、UINib、UIViewcontroller 的子类,尝试使用 UINib::nibWithNibName、UIViewController::initWithNibName、UIViewController::loadNibNamed 直接加载 NIB - 它确实如此没关系...我可以看到地图,但蓝点无论如何都没有显示...为什么??? 只有一种组合对我有用 - 以与控制器(MapController.xib)相同的方式命名 NIB 并将 MKMapView 的插座添加为控制器属性... 似乎不可能在一个类中加载 NIB / View 并使用它在另一个中,我对吗?
不知怎的,这工作得很好:
@interface MapController : UIViewController
@property (nonatomic, retain) IBOutlet MKMapView *mapView; // NIB was renamed to MapController.xib
...
@end
然后在控制器中:
self.mapView.showUserLocation = YES; // now this shows blue dot ... why???
谢谢,艺术
I have two classes - MapView and MapController. The main idea is to make View a shared object in MVC pattern.
- MapView has IBOutlet "mapView" for MKMapView located in MapNib.xib
- MapController contains property "mapView" referencing to class MapView
Then i try to set some properties of MKMapView from controller and it does not react on this anyway. For example i try in MapController :
self.mapView.mapView.showUserLocation = YES; // does not show blue point ...
MapView Class :
@interface MapView : UIViewController
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
...
//UINib *view = [UINib nibWithNibName:@"MapView" bundle:nil];
//[view instantiateWithOwner:self options:nil];
@end
MapController Class :
@interface MapController : UIViewController<
CLLocationManagerDelegate,
MKMapViewDelegate>
@property (nonatomic, retain) MapModel *mapModel;
@property (nonatomic, retain) MapView *mapView;
...
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView = [[[MapView alloc] init] autorelease];
self.mapView.mapView.showsUserLocation = YES;
} @end
I tried to make MapView a subclass of UIView, UINib, UIViewcontroller, tried to load NIB directly using UINib::nibWithNibName, UIViewController::initWithNibName, UIViewController::loadNibNamed - it does not matter ... i can see the map but blue point is not shown anyway ... why???
Only one combination works for me - to name NIB the same way as controller (MapController.xib) and add outlet for MKMapView as controller property ... it seems like it is impossible to load NIB / View in one class and use it in another, am i right?
Somehow this works fine :
@interface MapController : UIViewController
@property (nonatomic, retain) IBOutlet MKMapView *mapView; // NIB was renamed to MapController.xib
...
@end
And then in controller :
self.mapView.showUserLocation = YES; // now this shows blue dot ... why???
Thanks, Art
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不需要创建 IBOutlet。 NIB 创建它们并将它们分配给您的变量,您通过分配和初始化新的地图视图并将其保存到该变量来覆盖它
IBOutlets do not need to be created. The NIB creates them and assigns them to your vars, your are overwriting it by allocing and initing a new mapview and saving it to that variable