简体   繁体   English

iOS - 从appDelegate访问storyboard上的viewControllers

[英]iOS - Accessing viewControllers on storyboard from appDelegate

I've got an app using Core Data where I'm creating a managedObjectContext in the app delegate. 我有一个使用Core Data的应用程序,我在app delegate中创建了一个managedObjectContext

I want to pass that managedObjectContext to two view controllers on my storyboard so they are using the same managedObjectContext to save and fetch to and from. 我想将该managedObjectContext传递给我的故事板上的两个视图控制器,以便它们使用相同的managedObjectContext来保存和获取。

I can access the first view controller with: 我可以访问第一个视图控制器:

self.window.rootViewController

But the second view controller I want to access is then after a segue from the first and no reference is returned from it. 但是我要访问的第二个视图控制器是在第一个视图之后从第一个视图中返回,并且没有从它返回引用。

I tried: 我试过了:

instantiateViewControllerWithIdentifier:

But that creates a new instance of the view rather than allowing me to access the second view controller that appears after the segue. 但是这会创建一个新的视图实例,而不是允许我访问在segue之后出现的第二个视图控制器。

So my question is, how can I access the second view controller? 所以我的问题是,如何访问第二个视图控制器?

Or (as I'm very new to this) is there a better way to be managing/passing the data between the view controllers? 或者(因为我对此非常陌生)是否有更好的方法来管理/传递视图控制器之间的数据?

Thanks in advance. 提前致谢。

Or (as I'm very new to this) is there a better way to be managing/passing the data between the view controllers? 或者(因为我对此非常陌生)是否有更好的方法来管理/传递视图控制器之间的数据?

It depends on the data you're trying to pass around. 这取决于你试图传递的数据。 In this case, you want to give your view controllers access to your Core Data managed object context. 在这种情况下,您希望为视图控制器提供对Core Data托管对象上下文的访问权限。 Because this is something you're going to need throughout the lifespan of your app it would be better to have your view controllers access it via your application delegate. 因为这是您在应用程序的整个生命周期中所需要的,所以让视图控制器通过您的应用程序委托访问它会更好。

You can do this via [[UIApplication sharedApplication] delegate] - however, you may need to typecast it to avoid compiler warnings, or alternatively you might want to create a macro that returns the managed object context to save you time and make your code a little more readable. 您可以通过[[UIApplication sharedApplication] delegate]执行此操作 - 但是,您可能需要对其进行类型转换以避免编译器警告,或者您可能需要创建一个返回托管对象上下文的宏以节省您的时间并使您的代码成为更具可读性。

If you told XCode you wanted to use Core Data when you created the project you should have the methods to retrieve your object context already in your app delegate. 如果您在创建项目时告诉XCode您想要使用Core Data,那么您应该拥有在应用程序委托中检索对象上下文的方法。 If not, you'll need to create them. 如果没有,你需要创建它们。

To create a macro to save you having to write out [[UIApplication sharedApplication] delegate] every time you need to access the managed object context, check out this answer: Short hand for [[UIApplication sharedApplication] delegate]? 要创建一个宏以节省您每次需要访问托管对象上下文时必须写出[[UIApplication sharedApplication] delegate] ,请查看以下答案: [[UIApplication sharedApplication]委托]的简写?

You can go through this :- 你可以通过这个: -

  UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
  MasterViewController *result;

//check to see if navbar "get" worked
   if (navigationController.viewControllers) {

//look for the nav controller in tab bar views 
      for (UINavigationController *view in navigationController.viewControllers) {

    //when found, do the same thing to find the MasterViewController under the nav controller
         if ([view isKindOfClass:[UINavigationController class]])
             for (UIViewController *view2 in view.viewControllers) 
                 if ([view2 isKindOfClass:[MasterViewController class]])                    
                     result = (MasterViewController *) view2;


      }
   }

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

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