I have an iOS app I created as a "view-based app" in xCode. I have only one viewController, but it is displayed automatically, and I see no code that ties it to my appDelegate. I need to pass data from my appDelegate to my viewController, but don't know how to pull that off.
我在xCode中创建了一个iOS应用作为“基于视图的应用”。我只有一个viewController,但它是自动显示的,我没有看到将它与appDelegate绑定的代码。我需要将数据从appDelegate传递给我的viewController,但是不知道如何实现它。
My app delegate.h:
我的app delegate.h:
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSDictionary *queryStrings;
@end
Also, appDidFinishLoadingWithOptions:
同时,appDidFinishLoadingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[[JMC sharedInstance] configureJiraConnect:@"https://cmsmech.atlassian.net/" projectKey:@"WTUPLOAD" apiKey:@"7fc060e1-a795-4135-89c6-a7e8e64c4b13"];
if ([launchOptions objectForKey:UIApplicationLaunchOptionsURLKey] != nil) {
NSURL *url = [launchOptions objectForKey: UIApplicationLaunchOptionsURLKey];
NSLog(@"url received: %@", url);
NSLog(@"query string: %@", [url query]);
NSLog(@"host: %@", [url host]);
NSLog(@"url path: %@", [url path]);
queryStrings = [self parseQueryString:[url query]];
NSLog(@"query dictionary: %@", queryStrings);
}
else {
queryStrings = [self parseQueryString:@"wtID=nil"];
}
return YES;
}
6 个解决方案
#1
93
You can access it with:
你可透过以下途径查阅:
MyViewController* mainController = (MyViewController*) self.window.rootViewController;
If you are nesting your view behind a tabviewcontroller or navigation controller it will return that to you and you will need to access your view controller inside of it
如果你将视图嵌套在一个tabviewcontroller或导航控制器后面它会返回给你,你需要访问其中的视图控制器
#2
19
How about using good old fashioned NSNotifications to send a message from your app delegate to anyone listening (e.g. your view contorller) that something needs to be updated? or you can use Key Value Observing so your view controller can be watching some property in your app delegate.
如何使用老式的NSNotifications来向任何正在听(比如你的视图接触器)的人发送需要更新的消息?或者你可以使用键值观察这样你的视图控制器就可以观察你的应用委托中的一些属性。
#3
12
Since you only have one view controller, the generic way (independent of how your app was set up):
由于你只有一个视图控制器,通用的方式(独立于你的应用是如何设置的):
UIViewController *vc = [[[UIApplication sharedApplication] keyWindow] rootViewController];
#4
8
If you want to get any other UIViewController
, not just the rootViewController
:
如果你想获得任何其他的UIViewController,而不仅仅是rootViewController:
UIWindow *window=[UIApplication sharedApplication].keyWindow;
UIViewController *root = [window rootViewController];
UIStoryboard *storyboard = root.storyboard;
CustomViewController *vcc =(CustomViewController *) [storyboard instantiateViewControllerWithIdentifier:@"storyBoardID"];
#5
4
Swift way to do it, you can call this from anywhere, not just appdelegate:
快速的方法,你可以从任何地方调用它,而不仅仅是appdelegate:
/// EZSwiftExtensions - Gives you the VC on top so you can easily push your popups
public var topMostVC: UIViewController? {
var presentedVC = UIApplication.sharedApplication().keyWindow?.rootViewController
while let pVC = presentedVC?.presentedViewController {
presentedVC = pVC
}
if presentedVC == nil {
print("EZSwiftExtensions Error: You don't have any views set. You may be calling them in viewDidLoad. Try viewDidAppear instead.")
}
return presentedVC
}
Its included as a standard function in:
它作为标准函数包括在:
https://github.com/goktugyil/EZSwiftExtensions
https://github.com/goktugyil/EZSwiftExtensions
#6
0
If you used the View-Based Application template, the single view controller should be accessible via a property in your app delegate. It's the same view controller that gets set as the root view controller of the navigation controller.
如果您使用基于视图的应用程序模板,那么应该通过应用委托中的属性访问单个视图控制器。它是与导航控制器的根视图控制器相同的视图控制器。
If for some reason your project was set up differently, you can get the root view controller of the window, which should be the navigation controller, and then get its top view controller.
如果由于某些原因,您的项目设置不同,您可以获得窗口的根视图控制器,它应该是导航控制器,然后获取它的顶部视图控制器。
EDIT: So the issue is this: With iOS5 and storyboards, Apple has abstracted a lot of the initial set up that you used to have access to (and still do if you opt out of storyboards). They've altered the arguments passed to main() and do more of the set up in the storyboard (which is really just a nib). IMHO, this is in part to keep people from overloading the AppDelegate with heavy operations, like it appears you are doing in yours. The AppDelegate, in essence, exists to manage the Application lifecycle. It's not really meant to be performing methods that hand off information to your view controllers. It's your view controller's job, really, to do the heavy lifting to provide itself with data. I would suggest moving the code into your view controller, perhaps in viewDidLoad. If you need to know the result of the launchOptions objectForKey test it appears you're doing, you could very simply create a property on your AppDelegate, say BOOL launchOptionsURLKeyExists, and set it appropriately. Then you just grab the value of this property in your view controller. You AppDelegate is already a singleton, so you can access it either by [UIApplication sharedApplication].delegate
编辑:所以问题是这样的:通过iOS5和storyboards,苹果已经抽象了很多你曾经使用过的初始设置(如果你选择不使用storyboard的话)。他们修改了传递给main()的参数,并在故事板中做了更多的设置(实际上只是一个nib)。IMHO,这在一定程度上是为了防止人们用繁重的操作重载AppDelegate,就像你在你的应用中所做的一样。AppDelegate本质上是用来管理应用程序生命周期的。它并不是要执行将信息传递给视图控制器的方法。这是你的视图控制器的工作,真的,为了给自己提供数据。我建议将代码移动到视图控制器中,可能是在viewDidLoad中。如果您需要知道launchOptions objectForKey测试的结果,那么您可以在AppDelegate上创建一个属性,比如BOOL launchoptionsurlkeyexist,然后适当地设置它。然后在视图控制器中获取这个属性的值。您的AppDelegate已经是单例的,所以您可以通过[UIApplication sharedApplication].delegate访问它
EDIT 2: viewWillAppear/viewDidAppear gets called when the view is added to the UIApplicationDidEnterForegroundNotification notification in your view controller and respond appropriately when that message is posted.
编辑2:viewWillAppear/viewDidAppear在将视图添加到您的视图控制器中的uiapplicationdidentergroundnotification通知时被调用,并在消息发布时适当地响应。
#1
93
You can access it with:
你可透过以下途径查阅:
MyViewController* mainController = (MyViewController*) self.window.rootViewController;
If you are nesting your view behind a tabviewcontroller or navigation controller it will return that to you and you will need to access your view controller inside of it
如果你将视图嵌套在一个tabviewcontroller或导航控制器后面它会返回给你,你需要访问其中的视图控制器
#2
19
How about using good old fashioned NSNotifications to send a message from your app delegate to anyone listening (e.g. your view contorller) that something needs to be updated? or you can use Key Value Observing so your view controller can be watching some property in your app delegate.
如何使用老式的NSNotifications来向任何正在听(比如你的视图接触器)的人发送需要更新的消息?或者你可以使用键值观察这样你的视图控制器就可以观察你的应用委托中的一些属性。
#3
12
Since you only have one view controller, the generic way (independent of how your app was set up):
由于你只有一个视图控制器,通用的方式(独立于你的应用是如何设置的):
UIViewController *vc = [[[UIApplication sharedApplication] keyWindow] rootViewController];
#4
8
If you want to get any other UIViewController
, not just the rootViewController
:
如果你想获得任何其他的UIViewController,而不仅仅是rootViewController:
UIWindow *window=[UIApplication sharedApplication].keyWindow;
UIViewController *root = [window rootViewController];
UIStoryboard *storyboard = root.storyboard;
CustomViewController *vcc =(CustomViewController *) [storyboard instantiateViewControllerWithIdentifier:@"storyBoardID"];
#5
4
Swift way to do it, you can call this from anywhere, not just appdelegate:
快速的方法,你可以从任何地方调用它,而不仅仅是appdelegate:
/// EZSwiftExtensions - Gives you the VC on top so you can easily push your popups
public var topMostVC: UIViewController? {
var presentedVC = UIApplication.sharedApplication().keyWindow?.rootViewController
while let pVC = presentedVC?.presentedViewController {
presentedVC = pVC
}
if presentedVC == nil {
print("EZSwiftExtensions Error: You don't have any views set. You may be calling them in viewDidLoad. Try viewDidAppear instead.")
}
return presentedVC
}
Its included as a standard function in:
它作为标准函数包括在:
https://github.com/goktugyil/EZSwiftExtensions
https://github.com/goktugyil/EZSwiftExtensions
#6
0
If you used the View-Based Application template, the single view controller should be accessible via a property in your app delegate. It's the same view controller that gets set as the root view controller of the navigation controller.
如果您使用基于视图的应用程序模板,那么应该通过应用委托中的属性访问单个视图控制器。它是与导航控制器的根视图控制器相同的视图控制器。
If for some reason your project was set up differently, you can get the root view controller of the window, which should be the navigation controller, and then get its top view controller.
如果由于某些原因,您的项目设置不同,您可以获得窗口的根视图控制器,它应该是导航控制器,然后获取它的顶部视图控制器。
EDIT: So the issue is this: With iOS5 and storyboards, Apple has abstracted a lot of the initial set up that you used to have access to (and still do if you opt out of storyboards). They've altered the arguments passed to main() and do more of the set up in the storyboard (which is really just a nib). IMHO, this is in part to keep people from overloading the AppDelegate with heavy operations, like it appears you are doing in yours. The AppDelegate, in essence, exists to manage the Application lifecycle. It's not really meant to be performing methods that hand off information to your view controllers. It's your view controller's job, really, to do the heavy lifting to provide itself with data. I would suggest moving the code into your view controller, perhaps in viewDidLoad. If you need to know the result of the launchOptions objectForKey test it appears you're doing, you could very simply create a property on your AppDelegate, say BOOL launchOptionsURLKeyExists, and set it appropriately. Then you just grab the value of this property in your view controller. You AppDelegate is already a singleton, so you can access it either by [UIApplication sharedApplication].delegate
编辑:所以问题是这样的:通过iOS5和storyboards,苹果已经抽象了很多你曾经使用过的初始设置(如果你选择不使用storyboard的话)。他们修改了传递给main()的参数,并在故事板中做了更多的设置(实际上只是一个nib)。IMHO,这在一定程度上是为了防止人们用繁重的操作重载AppDelegate,就像你在你的应用中所做的一样。AppDelegate本质上是用来管理应用程序生命周期的。它并不是要执行将信息传递给视图控制器的方法。这是你的视图控制器的工作,真的,为了给自己提供数据。我建议将代码移动到视图控制器中,可能是在viewDidLoad中。如果您需要知道launchOptions objectForKey测试的结果,那么您可以在AppDelegate上创建一个属性,比如BOOL launchoptionsurlkeyexist,然后适当地设置它。然后在视图控制器中获取这个属性的值。您的AppDelegate已经是单例的,所以您可以通过[UIApplication sharedApplication].delegate访问它
EDIT 2: viewWillAppear/viewDidAppear gets called when the view is added to the UIApplicationDidEnterForegroundNotification notification in your view controller and respond appropriately when that message is posted.
编辑2:viewWillAppear/viewDidAppear在将视图添加到您的视图控制器中的uiapplicationdidentergroundnotification通知时被调用,并在消息发布时适当地响应。