【问题标题】:Xcode Master Detail Application UIWebView not loading contentXcode Master Detail Application UIWebView 未加载内容
【发布时间】:2015-03-10 13:59:06
【问题描述】:

我有一个 Master Detail 应用程序,在 RootViewController(master) 上,我使用 tableview 来显示链接,当用户单击其中一个单元格时,它应该使用适当的网页填充 detailview。

在 RootViewController 中,这里是我在 DidSelectRow 上使用的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"did select row");
    NSArray* reversedArray = [[objectData reverseObjectEnumerator] allObjects];
    NSDictionary *snapshot = [[NSDictionary alloc] init];
    snapshot = [reversedArray objectAtIndex:indexPath.row];

    DetailViewController *myCont = [[DetailViewController alloc] init];
    NSString *url = snapshot[@"url"];
    [myCont loadPage:url];


}

这里的详细视图是我必须加载网页的方法

- (void)loadPage:(NSString*)urlString
{

    NSLog(@"THE URL: %@",urlString);
    NSURLRequest* request=[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    NSLog(@"REQUEST: %@",request);

    //self.myWebView.scalesPageToFit = YES;
    [myWebView loadRequest:request]; 

}

在我的 Storyboard 中,我将 UIWebView 连接到我的 DetailViewController 以用于 webview 委托和 webview 出口。

页面永远不会加载

【问题讨论】:

    标签: ios xcode uiwebview uisplitviewcontroller


    【解决方案1】:

    您是否尝试过符合 UIWebViewDelegate 协议?

    UIWebViewDelegate 有这些回调可以帮助你调试:

    • webView:shouldStartLoadWithRequest:navigationType:
    • webViewDidStartLoad:
    • webViewDidFinishLoad:
    • webView:didFailLoadWithError:

    您需要做的就是找到segue 的标识符并将URL 字符串传递给DetailViewController,当您尝试segue 进入该控制器并在DetailViewController 的viewDidLoad() 中实际调用您的loadPage 方法时。

    在苹果提供的 Master-Detail 模板中,您可以像这样传递数据

    ** 记下 [controller setDetailItem: object] 你应该传递你的 URL 的地方。

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
       if ([[segue identifier] isEqualToString:@"showDetail"]) {
           NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
           NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
           DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
           [controller setDetailItem:object];
           controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
           controller.navigationItem.leftItemsSupplementBackButton = YES;
       }
    }
    

    【讨论】:

    • 这里有一个教程,您可以学习如何通过故事板在控制器之间传递数据。这将有很大帮助。 appcoda.com/…
    • 主/从模板是否有默认的 segue 标识符?使用普通的故事板连接,我可以指定一个 segue 标识符,但使用模板,它似乎没有该字段。这是故事板的截图i.imgur.com/byZ7Tsr.png
    • 我在看到你的照片后编辑了我的答案^@diaonic 看看
    • 默认segue是“showDetail”看看我修改的sn-p :)
    【解决方案2】:

    您正在创建一个新的 DetailViewController,而不是使用情节提要的 DetailViewController。 这一行是错误的: DetailViewController *myCont = [[DetailViewController alloc] init]

    【讨论】:

      【解决方案3】:

      “在我的 Storyboard 中,我的 UIWebView 连接到我的 用于 webview 委托和 webview 出口的 DetailViewController。"

      但在您的代码中,您似乎是手动创建详细信息视图控制器,而不是从情节提要。

      DetailViewController *myCont = [[DetailViewController alloc] init];
      

      你应该使用UIStoryboardSegue而不是alloc+init。

      尝试阅读这篇文章 - https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/UsingViewControllersinYourApplication/UsingViewControllersinYourApplication.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-13
        • 2014-04-03
        • 1970-01-01
        • 1970-01-01
        • 2012-01-13
        • 1970-01-01
        • 2012-12-16
        相关资源
        最近更新 更多