简体   繁体   English

访问其他类/对象中的方法和变量(对象在哪里)?

[英]Access to methods and variables in other classes/objects (where are the objects btw)?

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++. 好的,我是Objective-C / iPhone编程的新手,所以当我尝试做在C ++中非常容易的事情时,会出现一些问题。

I´m building a tab bar based iPhone application with three views, one for each tab bar button. 我正在构建一个基于标签栏的iPhone应用程序,该应用程序具有三个视图,每个标签栏按钮一个。 In the first view the user builds a NSdictionary, which the second view shall display as a graph. 用户在第一个视图中构建一个NSdictionary,第二个视图应将其显示为图形。 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. 要访问此词典,我将其保存到第一个视图控制器中的.plist中,然后在第二个视图控制器中从该.plist中构建新词典。

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. 为了制作图形视图,我使用了s7graphview,它在SecondViewController中进行了初始化等操作,但是有我自己导入的.h和.m文件。 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. 在GraphInfoList.m文件中实现了将值加载到图形中的方法(来自创建的字典),这意味着我必须从.plist创建另一个字典才能访问数据。 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. 在执行此操作的同时,我还创建了“ dataFilePath”方法,该方法返回.plist的文件路径,该文件路径用于将数据加载到字典中。 I have found no other way of implementing this method than to copy/paste it to every .m file that uses it! 除了将其复制/粘贴到使用它的每个.m文件中之外,我没有发现实现此方法的其他方法! There´s got to be another way? 还有另一种方式吗?

An while I´m at it: where are the objects in iPhone programming? 我有一阵子:iPhone编程中的对象在哪里? The .m files is classes, aren´t they? .m文件是类,不是吗? 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] ). 我从不使用new运算符创建新对象,并且我想如果我有任何要调用的对象(例如[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. 我真的不知道这在Obj-C / Cocoa中是如何处理的,我也不知道要寻找什么才能找到答案。 Help would be really appreciated. 帮助将不胜感激。

An object is an instance of a class (a .m file). 对象是类的实例(.m文件)。 You alloc/init a new object like you do in java with the new operator. 您可以像使用java中的new运算符那样分配/初始化一个新对象。 you can import the .h of a class you want to use/have access to in and then use something like 您可以导入要使用/有权访问的类的.h,然后使用类似

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). 至于dataFilePath方法...,您可以将其放在应用程序的委托中(可能类似于MyAppNameAppDelegate.m之类),该委托是一个单例(整个应用程序的一个实例)。 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. 我建议看一本入门的Objective-C书,以获取更多信息。 I'd suggest Learn Objective-C on the Mac . 我建议在Mac上学习Objective-C 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. 祝好运。

I had the same problem using s7graphview in a different view controller. 我在不同的视图控制器中使用s7graphview时遇到了相同的问题。 I solved it by adding an NSDictionary as a property in the AppDelegate and when the viewwilldisappear method fires i added this code: 我通过在AppDelegate中添加一个NSDictionary作为属性来解决它,并且当viewwilldisappear方法触发时,我添加了以下代码:

[((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 我认为这不是唯一的方法,但是您将删除使应用程序变慢的.plist代码

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. 不要忘记#import“ YourAppDelegate.h”文件。

.m files are like .cpp files or .c files. .m文件类似于.cpp文件或.c文件。 Basically, implementation files. 基本上是实现文件。

Using a NSDictionary is a bad replacement for proper domain objects, you get problems when juggling your keys. 使用NSDictionary不能很好地替代适当的域对象,在使用密钥时会遇到问题。 And often objects are not the best way to model your data, for example numbers as NSNumber is just cumbersome. 通常,对象不是建模数据的最佳方法,例如,数字很麻烦,因为NSNumber很麻烦。

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. 让选项卡栏中的两个控制器都可以访问同一对象没有问题,您可以轻松地使用viewWillAppear:方法将状态从一个视图更新到另一个视图。

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. 通常,您想要的就像Apple在其框架中使用的那样懒惰。 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. 从简单开始,因为这是一个好的开始,然后从那里开始构建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM