热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

获取指向另一个对象的成员的指针或引用-Gettingapointerorreferencetoamemberofanotherobject

Hihaveannewbieissuewithobjective-c,cocoa&iPhone.IvedeclaredmapViewinmyapplicatio

Hi have an newbie issue with objective-c, cocoa & iPhone. I've declared mapView in my application delegates applicationDidFinishLaunching:application: which is instance/pointer of MKMapView class. mapView is a member of my application delegate.

大家好,我是objective-c, cocoa & iPhone的新手。我在应用程序委托applicationdidfinishlaunch:application中声明了mapView:application:它是MKMapView类的实例/指针。mapView是我的应用程序委托的成员。

In my view controller in viewDidLoad: I get instance of my application delegate with the following:

在viewDidLoad的视图控制器中:我获得应用程序委托的实例,如下所示:

appDelegate = [[UIApplication sharedApplication] delegate];

My view controller also has got MKMapView *mapView as a member. In viewDidLoad: I try to set it this way:

我的视图控制器也有MKMapView *mapView作为成员。在viewDidLoad:我试图这样设置:

mapView = [appDelegate mapView]; 

but it seems that I'm not able to get pointer or "reference" to the actual mapView because when I try to [mapView setRegion:region animated:YES]; it does not work. How ever [[appDelegate mapView] setRegion:region animated:YES]; does work.

但似乎我无法获得指向实际mapView的指针或"reference"因为当我尝试[mapView setRegion:region animated:YES];它不工作。如何([appDelegate mapView] setRegion:区域动画:是的);做的工作。

So the question is how do I get a pointer to appDelegates mapView from my view controller?

问题是如何从我的视图控制器中获得指向appdelegate mapView的指针?

2 个解决方案

#1


1  

It sounds like you're caching your app delegate's mapView member. However, it's possible that at the time you perform this cache, it's not yet instantiated (setting a breakpoint at this location will reveal that to you).

听起来你在缓存应用委托的mapView成员。但是,在执行此缓存时,它可能还没有实例化(在此位置设置断点将向您揭示这一点)。

The answer to your question is: [appDelegate mapView] returns a pointer to the appView member. However, if that member is nil, that's what you'll get back.

您的问题的答案是:[appDelegate mapView]返回一个指向appView成员的指针。但是,如果这个元素是nil,你就会得到这个。

#2


-1  

You're not retaining the appDelegate. If you created your delegate like this:

不保留appDelegate。如果你像这样创建你的委托:

@property (retain) id appDelegate;

Then you have to actually call set on it to retain it:

然后你需要调用set来保留它

[self setAppDelegate:[[UIAppliation sharedApplication] delegate]];

Better yet, use assign in your declaration:

更好的是,在您的声明中使用assign:

@property (assign) id appDelegate;

Best regards,

最好的问候,


推荐阅读
author-avatar
贺bujak_491
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有