如何引用来自不同类的指针?

发布于 2024-09-05 18:50:48 字数 265 浏览 5 评论 0原文

首先,我非常鄙视单身人士。虽然我可能应该尝试使用一个,但我只是不想这样做。我想创建一个数据类(在加载时仅由视图控制器实例化一次),然后使用不同的类,从该数据实例中发送垃圾消息,直到它充满如此多的数据,它微笑。

那么,我该怎么做呢?当我实例化它时,我创建了一个指向数据类实例的指针。我现在在一个单独的视图控制器中,发生了操作,并且我想更新初始数据对象。我想我需要通过指针引用该对象,但我不知道该怎么做。是的,我已经设置了属性以及 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 技术交流群。

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

发布评论

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

评论(6

待天淡蓝洁白时 2024-09-12 18:50:48

如果您不喜欢该模式或者它不适合,则无需使用单例。假设您要在第一个视图控制器中创建第二个视图控制器,只需在第二个视图控制器中为模型对象声明一个 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.

反差帅 2024-09-12 18:50:48

为您的对象创建一个全局变量并在创建时将其存储在那里。您可以在 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.

寂寞清仓 2024-09-12 18:50:48

认真使用单例。如果您因为不知道代码而不喜欢它们:

@interface Order : NSObject {
NSMutableArray *order;
}

@property (nonatomic, retain) NSMutableArray *order;

+ (Order *)sharedInstance;

@end

#import "Order.h"


@implementation Order

@synthesize order;

+(Order *)sharedInstance {
static Order *myInstance = nil;

@synchronized(self) {
    if(!myInstance) {
        myInstance = [[Order alloc] init];
        }
    }
return myInstance;
}

-(void)dealloc {
    [order release];
    [super dealloc];

}
@end

Seriously use a singleton. In case you don't like them cause you don't know the code:

@interface Order : NSObject {
NSMutableArray *order;
}

@property (nonatomic, retain) NSMutableArray *order;

+ (Order *)sharedInstance;

@end

#import "Order.h"


@implementation Order

@synthesize order;

+(Order *)sharedInstance {
static Order *myInstance = nil;

@synchronized(self) {
    if(!myInstance) {
        myInstance = [[Order alloc] init];
        }
    }
return myInstance;
}

-(void)dealloc {
    [order release];
    [super dealloc];

}
@end
把人绕傻吧 2024-09-12 18:50:48

嗯。你好。 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.

萝莉病 2024-09-12 18:50:48

为什么不让它成为您的应用程序委托的属性?这样就不必使用单例模式,但您可以利用 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.

人疚 2024-09-12 18:50:48

不要忘记 Objective-C 是 C 的超集。

基本上,数据类是普通的 C struct

如果您想从另一个类访问该类的变量,请将其设置为全局变量。

mydata.h:

struct MyData {
    // Whatever data fields that you need, e.g.:
    NSInteger value;
};

extern struct MyData mydata;

mydata.c:

struct MyData mydata = {
    // Whatever initial value that you find relevant, e.g.:
    .value = 42;
};

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:

struct MyData {
    // Whatever data fields that you need, e.g.:
    NSInteger value;
};

extern struct MyData mydata;

mydata.c:

struct MyData mydata = {
    // Whatever initial value that you find relevant, e.g.:
    .value = 42;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文