当应用程序变为活动状态时,我的视图控制器中不会调用 viewWillAppear [英] viewWillAppear is not called in my view controller when the app becomes active

查看:21
本文介绍了当应用程序变为活动状态时,我的视图控制器中不会调用 viewWillAppear的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我是 iOS 菜鸟.

Disclaimer: I'm an iOS noob.

我使用 Xcode 的模板创建了一个基于视图的应用程序,用于将图片上传到网站.Xcode 自动为我创建了 AppDelegate.m (& .h) 和 ViewController.m 以及故事板.该应用程序是使用 URL 架构从网站启动的,我在我的视图控制器中使用该信息.我有两个问题:

I created a view based app using Xcode's template that uploads pictures to a website. Xcode created AppDelegate.m (& .h) and ViewController.m as well as a storyboard for me automatically. The app is launched from a website using a URL schema, and I use that information in my view controller. I have two questions:

1) 这是将查询字符串从我的 AppDelegate 传递到视图控制器的好方法吗?
我的 AppDelegate 中有一个名为 queryStrings 的 NSDictionary 属性,用于存储查询字符串.然后在我的视图控制器的 viewWillAppear 中,我使用以下代码访问它:

1) Is this a good way to pass query strings from my AppDelegate to the view controller?
I have a NSDictionary property in my AppDelegate called queryStrings which stores the query strings. Then in viewWillAppear in my view controller I use the following code to access it:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSDictionary *queryStrings = appDelegate.queryStrings;
    wtID = [queryStrings valueForKey:@"wtID"];

2) 当应用程序已经启动但在后台并且用户通过网页上的 url(使用不同的查询字符串)再次打开应用程序时,我已经在 AppDelegate 中实现了 openURL,它填充了我在 AppDelegate 中的 queryStrings 属性.问题是:永远不会调用视图控制器的 viewWillAppear 方法,因此不会执行上述代码.如何将查询字符串数据获取到视图控制器?视图控制器如何知道应用程序刚刚激活?

2) When the app is already launched but in background and the user opens the app again via a url on the webpage (with different query string), I have implemented openURL in the AppDelegate which populates my queryStrings property in the AppDelegate. Problem is: the view controller's viewWillAppear method is never called so the above code isn't executed. How do I get the query string data to the view controller? How does the view controller know the app just became active?

注意:因为项目是使用 Xcode 中的基于视图的应用程序"模板创建的,所以我的 AppDelegate 唯一的属性是 window 属性.我不知道我的视图控制器在代码中的哪个地方甚至被 AppDelegate 引用过......它永远不会被设置为 rootViewController 或代码中的任何东西......我想这只是一些我看不到的 IB 魔法.

NOTE: Because the project was created using the "view-based app" template in Xcode, the only property my AppDelegate has is the window property. I can't tell where in code that my view controller is ever even referenced by the AppDelegate... It's never set as the rootViewController or anything in the code anyway... I guess it's just some IB magic that I can't see.

如果有帮助,这是我的 AppDelegate.h:

In case it helps, here's my AppDelegate.h:

#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSDictionary *queryStrings;

@end

这是我的 didFinishLoadingWithOptions、openURL 和我的辅助方法:parseQueryString:

and here's my didFinishLoadingWithOptions, openURL, and my helper method: parseQueryString:

- (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;
}


-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    NSLog(@"openURL executed");
    if (!url) 
        return NO;
    NSLog(@"query string: %@", [url query]);
    queryStrings = [self parseQueryString:[url query]];

    mainViewController.wtID = [queryStrings valueForKey:@"wtID"];
    return YES;
}

-(NSDictionary *)parseQueryString:(NSString *)query
{
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCapacity:6];
    NSArray *pairs = [query componentsSeparatedByString:@"&"];
    for (NSString *pair in pairs)
    {
        NSArray *elements = [pair componentsSeparatedByString:@"="];
        NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [dictionary setObject:val forKey:key];
    }
    return dictionary;
}

提前致谢!

推荐答案

所以,故事板开始让我紧张,我什至没有构建您的应用程序!:)

So, storyboards are starting to get on my nerves, and I'm not even building your app! :)

正如我在您的另一个问题中所述(这是重复的,但是嘿),缺少对视图控制器的引用是由于情节提要.viewDidAppear 和 viewWillAppear 在视图添加到层​​次结构时被调用.往返于背景和前景并不符合被添加到层次结构的条件.幸运的是,您的应用会发送与 AppDelegate 中的方法相对应的通知.只需注册为 UIApplicationWillEnterForegroundNotification 通知的观察者即可.然后,您可以在收到通知后触发的方法中更新您的 UI!

As I have stated in your other question (of which this is a duplicate, but hey), the lack of the reference to the view controller is due to storyboards. viewDidAppear and viewWillAppear get called when the view is added to the hierarchy. Going to and from the background and foreground doesn't qualify as being added to the hierarchy. Luckily, your app sends notifications that correspond to the methods in the AppDelegate. Simply register as an observer of the UIApplicationWillEnterForegroundNotification notification. Then you can update your UI in the method that gets fired as a result of receiving that notification!

添加指向重复问题的链接以获得良好的衡量标准:如何从我的 appDelegate 访问我的 viewController?iOS

Adding the link to the duplicated question for good measure: How do I access my viewController from my appDelegate? iOS

这篇关于当应用程序变为活动状态时,我的视图控制器中不会调用 viewWillAppear的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆