简体   繁体   English

在Objective-C中共享对象

[英]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. 如果我有一个ViewController并且在该类中,我有一个名为UserInfo和其他ViewController的对象,我们只需将其称为X,Y,Z等.etc。

What do I need to do so that those X,Y,Z can use the information of UserInfo? 我需要做什么才能使那些X,Y,Z可以使用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. 好吧,我可以在X,Y,Z中有另一个名为UserInfo的信息,并在里面传递UserInfo,但我认为这不是一个好的OOP技术。 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 我认为这里需要继承...但我不认为它也是正确的,因为它们唯一的共同点就是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. 如果您在第一个视图控制器中,并且具有将其传递给下一个视图控制器的属性NSString *aString ,则只需使第二个视图控制器的NSString *aString属性等于第一个视图控制器。

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. 如果你继承了你继承的所有属性,而不是它们的值。

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

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