开发者

How do I reference a pointer from a different class?

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 w开发者_Go百科ay 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.


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.


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:

@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


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.


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.


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;
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜