如何引用来自不同类的指针?
首先,我非常鄙视单身人士。虽然我可能应该尝试使用一个,但我只是不想这样做。我想创建一个数据类(在加载时仅由视图控制器实例化一次),然后使用不同的类,从该数据实例中发送垃圾消息,直到它充满如此多的数据,它微笑。
那么,我该怎么做呢?当我实例化它时,我创建了一个指向数据类实例的指针。我现在在一个单独的视图控制器中,发生了操作,并且我想更新初始数据对象。我想我需要通过指针引用该对象,但我不知道该怎么做。是的,我已经设置了属性以及 getter 和 setter,它们似乎有效,但仅限于初始视图控制器类。
First off, I despise singletons with a passion. Though I should probably be trying to use one, I just don't want to. I want to create a data class (that is instantiated only once by a view controller on loading), and then using a different class, message the crap out of that data instance until it is brimming with so much data, it smiles.
So, how do I do that? I made a pointer to the instance of the data class when I instantiated it. I'm now over in a separate view controller, action occurs, and I want to update the initial data object. I think I need to reference that object by way of pointer, but I have no idea how to do that. Yes, I've set properties and getters and setters, which seem to work, but only in the initial view controller class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您不喜欢该模式或者它不适合,则无需使用单例。假设您要在第一个视图控制器中创建第二个视图控制器,只需在第二个视图控制器中为模型对象声明一个 ivar 和属性,然后在实例化它时,将模型对象分配给该属性。
There's no need to use a singleton if you don't like the pattern or if it doesn't fit. Assuming you are creating your second view controller in the first one, just declare an ivar and property for your model object in your second view controller and when you instantiate it, assign the model object to this property.
为您的对象创建一个全局变量并在创建时将其存储在那里。您可以在 init 方法中连接它(可能是不好的风格),或者从调用者或通过接口生成器连接。只需让您的变量在使用它的文件中为人所知即可。
或者 - 好吧 - 使用某种单例模式并直接从该类获取实例。看起来干净多了。
Make a global variable for your object and store it there on creation. You can wire that up in the init method (probably bad style), or from the caller or via interface builder. Just make your variable known in the files that use it.
Or - well - use some kind of singleton pattern and get the instance directly from that class. Looks much cleaner.
认真使用单例。如果您因为不知道代码而不喜欢它们:
Seriously use a singleton. In case you don't like them cause you don't know the code:
嗯。你好。 Core Data 对您来说不是一个足够好的框架吗?它允许您拥有单个持久存储和多个上下文来管理更新和合并更改以响应通知。我可能在这里有些不合时宜,但是看到您在第一个问题中如何以对已被广泛接受的模式的强烈意见来开始问题,表明您没有花太多时间来发现 iOS 中的 Objective C 运行时和 Foundation 类的方式可以协作完成任务。在任何软件中,一个对象并且只有一个对象拥有特定资源。你应该拥抱单身人士。我建议您花一些时间阅读http://www.cocoadesignpatterns.com/。哦对了,看看KVO的含义。
Um. Hello. Isn't Core Data a good enough framework for you? It allows you to have a single persistent store and multiple contexts to manage updates and merging of changes in response to notifications. I may be out of line here, but seeing how you start the question with a strong opinion about a well accepted pattern in your first question indicates that you have not spent much time discovering the ways in which the objective c runtime and Foundation classes in iOS can collaborate to achieve a task. In any software, one object and only one object owns a specific resource. You should embrace singletons. I suggest you spend some time reading http://www.cocoadesignpatterns.com/. Oh yeah, check out the meaning of KVO.
为什么不让它成为您的应用程序委托的属性?这样您就不必使用单例模式,但您可以利用 Apple 现有的单例模式用法。
Why not make it a property of your app delegate? That way you don't have to use the singleton pattern but you are taking advantage of Apple's already existing usage of the singleton pattern.
不要忘记 Objective-C 是 C 的超集。
基本上,数据类是普通的 C
struct
。如果您想从另一个类访问该类的变量,请将其设置为全局变量。
mydata.h:
mydata.c:
Don't forget that Objective-C is a superset of C.
Basically, a data class is a plain C
struct
.If you want to access a variable of that class from another class, make it global.
mydata.h:
mydata.c: