【问题标题】:Call ViewController Method from AppDelegate after URL Scheme OpenURL在 URL Scheme OpenURL 之后从 AppDelegate 调用 ViewController 方法
【发布时间】:2015-01-20 00:27:00
【问题描述】:

我正在尝试从 AppDelegate.m 调用 ViewController.m 中的函数 LoadWebpage

我已使用 URL 方案启用我的应用程序,这样 Safari 中的“urlscheme://?querystring”链接会打开我的应用程序。我正在 AppDelegate 中捕获 url 方案(我可以通过记录来判断这是有效的)并且想用查询字符串填充一个全局变量,然后调用我的 LoadWebPage 方法,以便视图控制器可以使用全局变量并打开WebView 控件中请求的网站。

我关注this tutorial

这是 AppDelegate.m:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    NSLog(@"Calling Application Bundle ID: %@", sourceApplication);
    NSLog(@"URL scheme:%@", [url scheme]);
    NSLog(@"URL query:%@", [url query]);
    URLString = (NSString *)[url query]; //extern variable

    [self.ViewController loadWebpage];
    return YES;

}

在 ViewController.m 中:

- (void)LoadWebpage{

    NSString *strURL=URLString;  //more logic will be required to get the appropriate url from the query string.
    NSURL *url = [NSURL URLWithString:strURL];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [self.webview loadRequest:urlRequest];

}

但是[self.ViewController loadWebpage]; 行有一个错误“在'AppDelegate' 类型的对象上找不到属性'ViewController'”。

我猜我需要合成对打开视图控制器的引用,但我找不到有关如何执行此操作的信息。这是最好的处理方式吗?

编辑:根据下面闫的评论,我尝试添加

ViewController* mainController = (ViewController*)  self.window.rootViewController;
[mainController LoadWebpage];

而不是[self.ViewController loadWebpage];,但它只是以lldb结束调试?

【问题讨论】:

  • 我想你的意思是写 self.viewController 让我知道这是否有效。
  • 这两种方式都不起作用。自动完成也没有任何大小写的“视图控制器”。
  • 看看这个问题stackoverflow.com/questions/10015567/…答案展示了如何访问根viewController
  • 我尝试这样做,然后调用 LoadWebpage 函数,但它只是结束了 lldb 的调试?抱歉,我对此不是很有经验,不知道这意味着什么或如何解决它。
  • 用确切的错误更新答案。或许能看到发生了什么。

标签: ios objective-c iphone xcode uiviewcontroller


【解决方案1】:

与其尝试让 AppDelegate 在您的视图控制器中触发某些东西,我建议让视图控制器注册以在应用程序启动时接收通知。然后,您可以检查您在 AppDelegate 中设置的变量并做出相应的反应。

例如:

YourViewController.m:

- (void)viewDidLoad {
    [super viewDidLoad];

    // This notification is sent the first time the app launches
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(applicationBecameActive:)
                                                 name: UIApplicationDidBecomeActiveNotification
                                               object: nil];

    // This notification is sent when the app resumes from the background
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(applicationEnteredForeground:)
                                                 name: UIApplicationWillEnterForegroundNotification
                                               object: nil];
}

【讨论】:

  • 谢谢。这是一个比我想要的更好的解决方案。
  • 在 iOS 6 (?) 之前,您必须在 viewWillAppear 中添加观察者,并在 viewWillDisappear 中移除观察者(以防视图被卸载)。调用 openURL 时,不会调用 viewWillAppear(这是预期的行为)。只是想知道在我们不得不移除观察者的那一天,模式会是什么?
【解决方案2】:

对于那些想要在 Swift 中使用它的人!

首先,创建 NotificationCenter post 方法(在 Swift 2.0 - NSNotification Center 中),您要发送数据:

    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NOTIFICATION_NAME"), object: nil)

    return true
}

在您想要接收数据的 ViewController 类中,在 super.viewDidLoad() 中添加以下内容:

NotificationCenter.default.addObserver(self,selector: #selector(self.YOUR_METHOD_NAME),
name: NSNotification.Name(rawValue: "NOTIFICATION_NAME"),
object: nil)

以及你要调用的方法:

func YOUR_METHOD_NAME(notification: NSNotification) {
    // Your method calling, or what you want to do with received data
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多