简体   繁体   English

Xcode从顶级对象获取对象

[英]Xcode Get Object from Top Level Object

In my iOS app setup, I have a universal object (lets call it UniversalObj ) declared in the top level object. 在我的iOS应用设置中,我在顶层对象中声明了一个通用对象(简称为UniversalObj )。 ( SampleAppDelegate in my case) (在我的情况下为SampleAppDelegate

Within this, I have a UITabBarController containing two UIViewController (which I want to have access to UniversalObj . 在其中,我有一个包含两个UIViewController的UITabBarController(我想访问UniversalObj

In SampleAppDelegate , I have tried the following to pass UniversalObj to it: SampleAppDelegate ,我尝试了以下方法将UniversalObj传递给它:

MyViewController *vc = (MyViewController *) [self.tabBarController.viewControllers.objectAtIndex:0];
[vc setMyObj:self.universalObject];

The problem with this is that when I do this, sometimes, the view hasn't loaded yet so MyViewController.myObj==null . 问题是,当我这样做时,有时视图尚未加载,因此MyViewController.myObj==null

Now, I am trying to access SampleAppDelegate (of type NSObject ) from MyViewController . 现在,我试图从MyViewController访问SampleAppDelegateNSObject类型)。 I can get to the UITabBarController using: ***self.parentViewController*** , but I don't know how to get to SampleAppDelegate . 我可以使用***self.parentViewController***到达UITabBarController ,但是我不知道如何到达SampleAppDelegate

How can I access UniversalObj in SampleAppDelegate from MyViewController ? 如何从MyViewController访问SampleAppDelegate UniversalObj How can I change the self.parentViewController line in MyViewController to get to SampleAppDelegate ? 如何更改MyViewControllerself.parentViewController行以获取SampleAppDelegate Or is there a better way to do this? 还是有更好的方法来做到这一点?

Thanks in advance for your help. 在此先感谢您的帮助。

Guvvy 古维

您将使用-[UINib instantiateWithOwner:options:]

NSArray * topLevelObjects = [nib instantiateWithOwner:pwner options:options];

我发现可以像这样从MyViewController访问对象:

[self setMyObj:[(SampleAppDelegate *)[UIApplication sharedApplication].delegate universalObj]];

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

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