从 NIB 加载/创建 GeoLocation 视图 - IOS 和 MapKit

发布于 2024-12-20 01:09:54 字数 1761 浏览 5 评论 0原文

我有两个类 - MapView 和 MapController。主要思想是使View成为MVC模式中的共享对象。

  1. MapView 具有位于 MapNib.xib 中的 MKMapView 的 IBOutlet“mapView”
  2. 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.

  1. MapView has IBOutlet "mapView" for MKMapView located in MapNib.xib
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风几经秋 2024-12-27 01:09:54

不需要创建 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文