0

For example, if I create an UILabel in an class, how can I use it wherever I want to?

I already know how to share between two classes. But I need to call the value of an object in all my classes. Is it possible?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Panecillo
  • 43
  • 2
  • 8
  • Sounds like a really bad idea to me. See [Spaghetti Code](http://en.wikipedia.org/wiki/Spagetti_code) – vikingosegundo Oct 01 '11 at 11:54
  • Not clear: Do you want to simply share a class definition, or create an object instance and have it accessible from anywhere in your app. For the former you use .h files. For the latter you use your subclass of UIApplicationDelegate. Or you can use a singleton, but that a whole 'nother can of worms. – Hot Licks Oct 01 '11 at 12:13
  • I guess I assumed he meant sharing data between objects because it was asked how to call a value in all classes. But, as I re-read it is unclear because the UILabel ref asks about it using it wherever. Can you clarify? – bryanmac Oct 01 '11 at 12:20
  • First sorry my bad english. Yes, I meant assigning the text of an object's property (UILabel) of a class with the UILabel of an other class. – Panecillo Oct 01 '11 at 16:36

3 Answers3

1

Is a singleton what you want? Basically a singleton is just a class that only returns the same instance no matter what. Here's a website that shows you how to do them. http://funwithobjc.tumblr.com/post/3478903440/how-i-do-my-singletons

TheAmateurProgrammer
  • 9,252
  • 8
  • 52
  • 71
0

The best way is to write it to the temporary files folder like this:

NSString *pathtmp = [NSTemporaryDirectory() stringByAppendingPathComponent:@"tmpString.txt"];
NSString *stringToWrite = [NSString stringWithFormat:@"%@",Label.text];
[stringToWrite writeToFile:pathtmp atomically:YES encoding:NSStringEncodingConversionExternalRepresentation error:nil];

and to read it:

NSString *pathtmp = [NSTemporaryDirectory() stringByAppendingPathComponent:@"tmpString.txt"];
NSString *stringToWrite = [NSString stringWithContentsOfFile:pathtmp encoding:NSStringEncodingConversionExternalRepresentation error:nil];
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
JonasG
  • 9,274
  • 12
  • 59
  • 88
0

You asked that "I need to call the value of an object in all my classes. Is it possible?" and mentioned a UILabel.

You should really avoid view layer components poking at other view layer components. It creates coupling which makes it (1) really hard to change - simple changes break large areas of the code in unpredictable ways (see ref to spaggeti code above) and (2) hard to test since there's no layers in the system.

You should look into the principles of MVC. In MVC, you have views, controllers and models. You should push as much down as possible. Multiple views and controllers can operate on the same model that controls the data and business logic.

The model is the data and the operations you perform on the data. Make all you different views work off that data instead of poking at each other.

I would suggest create a set of model classes and to allow common access to that model. A common pattern is for that class to be a singleton.

So, the multiple views & controllers would do.

MyModel *model = [MyModel sharedInstance]; Then multiple controllers can operate on it.

Here's a good article on the topic: http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html

singleton from apple: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32

Hope that helps.

bryanmac
  • 38,941
  • 11
  • 91
  • 99