在 Cocoa 中的两个控制器之间同步一个对象
我有一个 MainController,它创建一个对象(例如多边形),一个控制器(例如,polygonViewController)。 PolygonViewController 有两个出口:
IBOutlet Polygon* aPolygon;
IBOutlet UILabel* numOfSidesLabel;
它协调 Polygon 和 PolygonViewController.xib 文件。
如何确保 PolygonViewController 的 aPolygon 与 mainController 中创建的多边形相同(实例)?
我尝试将 @property Polygon* aPolygon; 添加到 PolygonViewController 然后在 MainController 中我做了 polygonViewController.aPolygon = Polygon;
但是,PolygonViewController awakeFromNib 方法中的 aPolygon 始终为 null。
I have a MainController, it creates an object(say polygon), a controller(say,polygonViewController). PolygonViewController has two outlets:
IBOutlet Polygon* aPolygon;
IBOutlet UILabel* numOfSidesLabel;
it mediates Polygon and PolygonViewController.xib file.
How do I make sure the PolygonViewController's aPolygon is the same(instance) as polygon created in mainController?
I tried to add @property Polygon* aPolygon;
to PolygonViewController then in MainController I did polygonViewController.aPolygon = polygon;
However, aPolygon in PolygonViewController awakeFromNib method is always null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过使用单例对象?
您可以将 aPolygon 类设置为单例对象,这样您就可以在任意数量的 viewController 中始终使用同一个对象。
看一下: http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32
希望这对您有帮助!
干杯,
虚拟网络
Have you tried to use a singleton object?
You can make your aPolygon class to be a singleton object so, you will be always working with the same object in as many viewControllers as you want to use it.
Take a look at: http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32
Hope this will help you!
Cheers,
VFN