热门标签 | 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,

最好的问候,


推荐阅读
  • Flutter 核心技术与混合开发模式深入解析
    本文深入探讨了 Flutter 的核心技术,特别是其混合开发模式,包括统一管理模式和三端分离模式,以及混合栈原理。通过对比不同模式的优缺点,帮助开发者选择最适合项目的混合开发策略。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
  • 重写init方法后,属性声明后为什么不为nil? ... [详细]
  • 在软件开发过程中,经常需要将多个项目或模块进行集成和调试,尤其是当项目依赖于第三方开源库(如Cordova、CocoaPods)时。本文介绍了如何在Xcode中高效地进行多项目联合调试,分享了一些实用的技巧和最佳实践,帮助开发者解决常见的调试难题,提高开发效率。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 使用GDI的一些AIP函数我们可以轻易的绘制出简 ... [详细]
  • 实体映射最强工具类:MapStruct真香 ... [详细]
  • 在尝试使用C# Windows Forms客户端通过SignalR连接到ASP.NET服务器时,遇到了内部服务器错误(500)。本文将详细探讨问题的原因及解决方案。 ... [详细]
  • 作为一名在大型手机游戏公司工作的程序员,尽管主要负责游戏逻辑和内容的开发,但对iOS底层开发接触较少。现在有了iPhone和可以虚拟MAC环境的电脑,希望能找到有效的iOS开发学习路径。 ... [详细]
  • 本文详细介绍了Objective-C中的面向对象编程概念,重点探讨了类的定义、方法的实现、对象的创建与销毁等内容,旨在帮助开发者更好地理解和应用Objective-C的面向对象特性。 ... [详细]
  • IOS Run loop详解
    为什么80%的码农都做不了架构师?转自http:blog.csdn.netztp800201articledetails9240913感谢作者分享Objecti ... [详细]
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社区 版权所有