简体   繁体   English

故事板中视图控制器之间的共享实例

[英]Shared instance between view controllers in storyboard

I have an app with portrait and landscape orientation. 我有一个纵向和横向的应用程序。 In storyboard I have two view controllers, both using the same subclass "genericVC", and a root view controller which is the initial view controller. 在故事板中,我有两个视图控制器,它们都使用相同的子类“genericVC”,以及一个根视图控制器,它是初始视图控制器。

When the app starts, the rootVC will instantiate both controllers and push one of them based on detected orientation. 当应用程序启动时,rootVC将实例化两个控制器并根据检测到的方向推送其中一个控制器。 I instantiate them using the method below (rootVC - ViewDidLoad). 我使用下面的方法(rootVC - ViewDidLoad)实例化它们。

    self.landscapeVC = [self.storyboard instantiateViewControllerWithIdentifier:@"iPadLandscapeVC"];
    self.portraitVC = [self.storyboard instantiateViewControllerWithIdentifier:@"iPadPortraitVC"];

What I need is somehow a shared instance for my genericVC class between self.landscapeVC and self.portraitVC, so I don't need to move around the data between them each time I rotate the device. 我需要的是self.landscapeVC和self.portraitVC之间的genericVC类的共享实例,所以每次旋转设备时我都不需要在它们之间移动数据。

Please advice me how can be done. 请指教我怎么做。

Thanks, Calin 谢谢,卡林

You want one view controller with two views, a portrait and a landscape. 您想要一个具有两个视图的视图控制器,纵向和横向。

You can add extra nibs or if you are using StoryBoard add an extra view controller in the storyBoard, but give both StoryBoard view controller nibs the same but the viewController class. 你可以添加额外的笔尖,或者如果你使用的是StoryBoard,在storyBoard中添加一个额外的视图控制器,但是同时给两个StoryBoard视图控制器的笔尖都是viewController类。

It's a bit of pain in StoryBoard if you accidentally switch between portrait and landscape when creating it it mangles things, and you only have access to one at a time. 如果你在创建它时不小心在纵向和横向之间切换它会破坏东西,那么在StoryBoard中会有点痛苦,你一次只能访问一个。 You have to drag one out, and one in to switch which one you are working on 你必须拖出一个,然后一个来切换你正在使用的那个

Apple has an example app https://developer.apple.com/library/ios/samplecode/AlternateViews/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008755 Apple有一个示例应用程序https://developer.apple.com/library/ios/samplecode/AlternateViews/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008755

Then what you want is a singleton that you can access at anytime during your application life cycle. 那么你想要的是一个单例,你可以在应用程序生命周期的任何时候访问它。 I won't recommend it as it will stay in memory for the entire app life cycle and it will consume resources that you might not be using at that time. 我不会推荐它,因为它将在整个应用程序生命周期中保留在内存中,并且它将消耗您当时可能没有使用的资源。

But as you don't want to share the info using properties between view controllers instances I would suggest you to read this. 但是,由于您不希望使用视图控制器实例之间的属性共享信息,我建议您阅读此内容。

https://developer.apple.com/library/ios/documentation/general/conceptual/DevPedia-CocoaCore/Singleton.html https://developer.apple.com/library/ios/documentation/general/conceptual/DevPedia-CocoaCore/Singleton.html

Another possible solution will be to send a dictionary between view controllers using notifications or delegates between them. 另一种可能的解决方案是使用通知或它们之间的委托在视图控制器之间发送字典。

Hope this helps 希望这可以帮助

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

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