0

视图控制器

  • UIWebView: webView- 一个简单的 UIWebView
  • UIButton: aboutButton- 带你到 AboutViewController

关于ViewController

  • UIButton: websiteButton- 连接到clickWebsiteButton
  • IBAction: clickWebsiteButton- 关闭 AboutViewController,加载(http://websiteURL.com/webViewViewController 中)

关于ViewController 代码

// AboutViewController.h

#import "ViewController.h"

@class ViewController;

@interface AboutViewController : UITableViewController <UIWebViewDelegate> {
    ViewController *viewController;
}


// AboutViewController.m

-(IBAction)clickWebsiteButton:(id)sender {
    [viewController.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://websiteURL.com/"]]];
    [self dismissModalViewControllerAnimated:YES];
}

问题

我希望能够http://websiteURL.com/在通过 IBAction 关闭视图后在 UIWebView 中加载。到目前为止,它所做的只是关闭视图,而不是在 WebView 中加载 URL。WebView 正在工作并正确加载 URL,我只是在从其他视图加载此 URL 时遇到了麻烦。有任何想法吗?

谢谢

4

3 回答 3

1

我回答了您关于持久数据存储的其他问题。这是让您的 viewControllers 共享数据的另一种方式,因此您可能不再需要它,但以防万一......

问题是您在关闭呈现的 viewController (aboutViewController) 之前在呈现的 viewController 上调用了一个方法。它需要在关闭过程完成后调用。

这种方法:

dismissModalViewControllerAnimated:

在 iOS6 中已弃用,从 iOS5 开始鼓励您改用它

dismissViewControllerAnimated:completion:

wherecompletion接受一个块参数。您放置在完成块中的代码将在关闭完成后执行。您可以在此处向呈现的 viewController 发送消息。

self.presentingViewController是对呈现 aboutViewController 的 viewController 的引用 - 它由 iOS 作为呈现过程的一部分提供。但是你不能在完成块中使用它,因为它在解除过程中被清空,所以你需要先将它复制到一个局部变量中。

在 aboutViewController...

-(IBAction)clickWebsiteButton:(id)sender 
{
        //to use self.presentingViewController in the completion block
        //you must first copy it to a local variable 
        //as it is cleared by the dismissing process 

    UIViewController* presentingVC = self.presentingViewController;

    [self.presentingViewController dismissViewControllerAnimated:YES
                                     completion:
     ^{
         if ([presentingVC respondsToSelector:@selector(loadRequestWithString:)]) {
             [presentingVC performSelector:@selector(loadRequestWithString:) 
                                withObject:@"http://websiteURL.com/"];
         }
     }];
}

在您呈现的 viewController 中,创建一个接受字符串参数的方法:

- (void) loadRequestWithString:(NSString*)webString
{
    NSURL* requestURL = [NSURL URLWithString:webString];
    [self.webView loadRequest:[NSURLRequest requestWithURL:requestURL]];


}
于 2013-03-27T04:49:30.567 回答
0

一种选择是使用委托回调。使用您当前的代码, viewController 瞬间为零。我有一个如何在此处实现委托模式的示例。

于 2013-03-27T01:36:46.167 回答
-1

请记住,如果您使用的是 UINavigationController,则必须这样做

UINavigationController *viewConNav = (UINavigationController *)self.presentingViewController;
YourVC *viewCon = (YourVC *)viewConNav.topViewController;
于 2013-07-17T13:05:10.913 回答