访问其他类/对象中的方法和变量(顺便说一句,对象在哪里)?

发布于 2024-11-07 01:59:53 字数 728 浏览 4 评论 0原文

好吧,我是 Objective-C/iPhone 编程的新手,所以当我尝试做一些在 C++ 中很容易的事情时,会出现一些问题。

我正在构建一个基于选项卡栏的 iPhone 应用程序,具有三个视图,每个选项卡栏按钮一个视图。在第一个视图中,用户构建一个 NSdictionary,第二个视图应将其显示为图形。为了访问这个字典,我将其保存到第一个视图控制器中的 .plist 中,然后在第二个视图控制器中从此 .plist 中构建一个新字典。

为了制作图形视图,我使用 s7graphview,它在 SecondViewController 中初始化等,但有自己的 .h 和 .m 文件,我将其导入。将值加载到图表中的方法(来自创建的字典)是在 GraphInfoList.m 文件中实现的,这意味着我必须从 .plist 中创建另一个字典才能访问数据。如何访问已经创建的词典?

在这样做的同时,我还创建了一个方法“dataFilePath”,它返回 .plist 的文件路径,我用它来将数据加载到字典中。除了将其复制/粘贴到每个使用它的 .m 文件之外,我没有找到其他实现此方法的方法!一定还有别的办法吗?

我想了一会儿:iPhone 编程中的对象在哪里? .m 文件是类,不是吗?我从不使用 new 运算符创建新对象,并且我认为如果我有任何要调用的对象(例如 [FirstViewController dataFilePath] ),我可能能够访问这些方法。

我真的不知道 Obj-C/Cocoa 中是如何处理的,也不知道要搜索什么来找到答案。非常感谢您的帮助。

Ok, I am new to Objective-C/iPhone programming, so there are some questions arising when I try to do things that would be quite easy in C++.

I´m building a tab bar based iPhone application with three views, one for each tab bar button. In the first view the user builds a NSdictionary, which the second view shall display as a graph. To access this dictionary, I save it to a .plist in the first view controller, then bulding a new dictionary from this .plist in the second.

To make the graph view, i use s7graphview, which is initialized etc in SecondViewController, but has its own .h and .m files, which I import. The method to load values into the graph (which is from the created dictionary), is implemented in the GraphInfoList.m file, which means I have to make another dictionary from the .plist to access the data. How can I access the already created dictionary?

While doing this, I also made a method "dataFilePath", which returns the file path of the .plist, which I use to load the data into a dictionary. I have found no other way of implementing this method than to copy/paste it to every .m file that uses it! There´s got to be another way?

An while I´m at it: where are the objects in iPhone programming? The .m files is classes, aren´t they? I never create a new object using the new operator, and I thought I may would be able to access the methods if I had any object to call (like [FirstViewController dataFilePath] ).

I really don´t know how this is handled in Obj-C/Cocoa, and I don´t know what to search for to find the answers. Help would be really appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

蘸点软妹酱 2024-11-14 01:59:53

对象是类的实例(.m 文件)。您可以像在 java 中使用 new 运算符一样分配/初始化一个新对象。您可以导入要使用/有权访问的类的 .h,然后使用类似

MyClass *objectInstance = [[MyClass alloc] init]; 

创建实例之类的方法。

至于 dataFilePath 方法...您可以将它放在应用程序的委托中(可能类似于 MyAppNameAppDelegate.m),它是一个单例(整个应用程序的一个实例)。然后,您可以使用以下方法获取文件路径:

myFilePath = [[[UIApplication sharedApplication] delegate] dataFilePath];

我建议您查看一本 Objective-C 入门书籍以获取更多信息。我建议在 Mac 上学习 Objective-C。另外,我会看一下面向对象编程的基本介绍,因为这似乎是最让你困惑的地方。祝你好运。

An object is an instance of a class (a .m file). You alloc/init a new object like you do in java with the new operator. you can import the .h of a class you want to use/have access to in and then use something like

MyClass *objectInstance = [[MyClass alloc] init]; 

to create an instance.

As far as the dataFilePath method... you can just have it in your app's delegate (probably something like MyAppNameAppDelegate.m) which is a singleton (one instance for the entire app). you can then get the file path using:

myFilePath = [[[UIApplication sharedApplication] delegate] dataFilePath];

I would suggest looking into a beginning objective-c book for more information. I'd suggest Learn Objective-C on the Mac. Also, I would take a look at a basic introduction to Object Oriented Programming, as it seems this is what is tripping you up more than anything. Good luck.

安穩 2024-11-14 01:59:53

我在不同的视图控制器中使用 s7graphview 遇到了同样的问题。我通过在 AppDelegate 中添加 NSDictionary 作为属性来解决这个问题,当 viewwilldisappear 方法触发时,我添加了以下代码:

[((MyAppDelegate*) [[UIApplication sharedApplication] delegate]) setDictionary:self.dictionary];

我认为这不是唯一的方法,但您将删除会减慢应用程序速度的 .plist 代码

I had the same problem using s7graphview in a different view controller. I solved it by adding an NSDictionary as a property in the AppDelegate and when the viewwilldisappear method fires i added this code:

[((MyAppDelegate*) [[UIApplication sharedApplication] delegate]) setDictionary:self.dictionary];

I think it isn't the only way to do it but you will remove the .plist code who slows you app

厌味 2024-11-14 01:59:53

您的应用程序委托是分享内容的好地方。您可以将数据文件路径函数保留在应用程序委托中并访问它

path = [[[UIApplication sharedApplication] delegate] dataFilePath];

不要忘记#import“YourAppDelegate.h”文件。

.m 文件类似于 .cpp 文件或 .c 文件。基本上是实施文件。

Your app delegate is a good place to share things. you can keep the data file path function in your app delegate and access it

path = [[[UIApplication sharedApplication] delegate] dataFilePath];

Dont forget to #import "YourAppDelegate.h" file.

.m files are like .cpp files or .c files. Basically, implementation files.

凉世弥音 2024-11-14 01:59:53

使用 NSDictionary 是正确域对象的糟糕替代品,在处理密钥时会遇到问题。通常,对象并不是对数据进行建模的最佳方式,例如 NSNumber 中的数字非常麻烦。

相反,引入一个适当的域类。让选项卡栏中的两个控制器访问同一对象是没有问题的,您可以轻松地使用 viewWillAppear: 方法将状态从一个视图更新到另一个视图。

甚至可能有一段时间你想要一个单身人士。如果任何对象只能有一个逻辑实例,那么单例就是最佳选择。不要害怕他们。通常,正如 Apple 在其框架中使用的那样,惰性模式就是您想要的。例如这个接口:

@interface MyDomainManager : NSObject { /* ivars here */ }

+(MyDomainManager*)sharedManager;

// More proper tie and method here    

@end

然后是这样的实现:

@implementation MyDomainManager

+(MyDomainManager*)sharedManager;
{
    static MyDomainManager* manager = nil;
    if (manager == nil) {
        manager = [[self alloc] init];
    }
    return manager;
}

// More implementation cruft…

@end

从简单开始,因为这是一个好的开始,然后从那里开始构建。

Using a NSDictionary is a bad replacement for proper domain objects, you get problems when juggling your keys. And often objects are not the best way to model your data, for example numbers as NSNumber is just cumbersome.

Instead introduce a proper domain class. There is not problem in letting both via controllers in your tab bar have access to the same object, you can easily use the viewWillAppear: method to update states from one view to the other.

There might even be a time when you want a singleton. If there can be only one logical instance of any object, the singleton is the way to go. Don't fear them. Usually a lazy patters just as Apple use in their frameworks is what you want. With for example this interface:

@interface MyDomainManager : NSObject { /* ivars here */ }

+(MyDomainManager*)sharedManager;

// More proper tie and method here    

@end

And then an implementation like this:

@implementation MyDomainManager

+(MyDomainManager*)sharedManager;
{
    static MyDomainManager* manager = nil;
    if (manager == nil) {
        manager = [[self alloc] init];
    }
    return manager;
}

// More implementation cruft…

@end

Starting out simple as this is a good start, and then build from there.

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