【问题标题】:My first iOS app. I have a UIWebView in my ViewController that I want to update我的第一个 iOS 应用。我的 ViewController 中有一个要更新的 UIWebView
【发布时间】:2014-07-17 20:43:58
【问题描述】:

我的ViewController 中有一个UIWebView,我想在UIApplicationDelegate 传递一个URL 时更新它,例如

myApp://?changeWebView=newstuff

我正在根据传入的数据构建新的 URL,但我不知道如何更新我的 UIWebView

我的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]);
    NSLog(@"URL hash: %@", [url fragment]);

    NSURL *newURL = [NSURL URLWithString:@"URL constructed with pieces of passed URL data"];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:newURL];


 //now I want to update the UIWebview defined in my ViewController.h

    return YES;
}

【问题讨论】:

    标签: ios uiwebview appdelegate uiapplicationdelegate uiwebviewdelegate


    【解决方案1】:

    您可以使用loadRequestwebView 中加载请求

    [self.myWebView loadRequest:requestObj]; 将此行放入您的代码中

    您可以发布通知,让您的视图控制器知道您可以加载 webview。

    - (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]);
        NSLog(@"URL hash: %@", [url fragment]);
    
        NSURL *newURL = [NSURL URLWithString:@"URL constructed with pieces of passed URL data"];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:newURL];
    
          //Post Notification
          [[NSNotificationCenter defaultCenter] postNotificationName:@"loadRequest"
                                      object:nil
                                      userInfo:@{@"requestObj":requestObj}];
    
        return YES;
    }
    

    现在在你的 viewController 中添加通知观察者

    -(void)viewDidLoad{
    
            //Add observer for notification
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveEvent:) name:@"loadRequest" object:nil];
    }
    

    编写此方法以在您的ViewController 中加载webView 中的请求

    - (void)receiveEvent:(NSNotification *)notification {
        // handle event
            NSURLRequest *requestObj = notification.userInfo[@"requestObj"];
           [self.myWebView loadRequest:requestObj];         
    }
    

    【讨论】:

    • 这行不通。他在应用程序委托中收到通知,但 Web 视图位于视图控制器中。
    【解决方案2】:

    对于初学者,由于您在应用程序委托中执行此操作,因此您不知道将分配带有 Web 视图的视图控制器。因此,您有几个不同的选择。

    • 首先,您可以在应用程序委托中创建一个属性 (@property (nonatomic, weak) UIViewController *viewController;)。当您的视图控制器加载时,将此属性设置为带有 Web 视图的视图控制器,以便您在应用委托中对其进行引用。
    • 其次,您可以发送一个NSNotification,它也会发送网址。这样做的好处是您可能希望将此 url 发送到 NSNotification 允许的许多不同的类。每个类都可以单独添加自己作为通知的观察者。这是一个例子:Send and receive messages through NSNotificationCenter in Objective-C?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-25
      • 2014-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多