简体   繁体   中英

sharing objects in Objective-C

This is a noob question. If I have a ViewController and inside that class I have an object called UserInfo and other ViewController, lets just call it X,Y,Z, etc..etc.

What do I need to do so that those X,Y,Z can use the information of UserInfo?

Well I can have another info called UserInfo inside X,Y,Z and pass UserInfo inside, but I don't think this is good OOP technique. I think inheritance is needed here... but I don't think that it is right too, as the only commonality they have is only the UserInfo

View controllers are just objects, you have to remember that.

If you're in your first view controller and it has a property NSString *aString to pass it to the next view controller you just make the second view controller's NSString *aString property equal to the first one.

Basically do this:

MyNextViewController *viewControllerTwo = [[MyNextViewController alloc] initWithNibName:nil bundle:nil];
[viewControllerTwo setAString:self.aString];

if you subclass all you inherit are the properties, and not their values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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