加载UIWebView时出现问题

时间:2015-02-16 19:13:35

标签: ios objective-c uiwebview uisplitview

我试图编写一个SplitView控制程序,其中按下masterViewController中的表格单元格会导致关联的网页加载到详细视图控制器中。我在详细视图控制器中有以下方法,我可以确认它已被调用并正在接收正确的输入:

  -(void)masterAction:(id)sender  {

    NSString *http = @"http://";
    http = [http stringByAppendingString:sender];
    _urlString = http;


    NSURL *url= [NSURL URLWithString:_urlString];
    [self.web loadRequest:[NSURLRequest requestWithURL:url]];

     }

然而,没有任何东西正在加载。任何想法为什么会这样?我能够加载任何东西的唯一方法是在我的viewDidLoad方法中插入类似于以下内容的东西:

NSURL *url= [NSURL URLWithString:@"http://www.google.com];
[self.web loadRequest:[NSURLRequest requestWithURL:url]];

使用以下方法调用该方法:

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *thread = [self.issueData objectForKey:@"responseData"];
    NSDictionary *feed = [thread objectForKey:@"feed"];


    NSArray *entries = [feed objectForKey:@"entries"];
    NSDictionary *posts = entries[indexPath.row];

    NSString *urlString = [posts objectForKey:@"link"];
    NSArray *split = [urlString componentsSeparatedByString:@"url=http://"];

    NSString   *url = [split objectAtIndex:1];

    [self.delegate masterAction:url];

}

2 个答案:

答案 0 :(得分:0)

我在测试项目中复制了这段代码,而唯一可能出错的代码就是忘记放入www。在域名之前的http://之后。 尝试将masterAction方法更改为以下方法:

- (void) masterAction: (id) sender
{
    if (![sender isKindOfClass:[NSString class]]) return;
    NSString *http = @"http://www.";
    NSString *urlString = [http stringByAppendingString:sender];
    NSURL *url = [NSURL URLWithString:urlString];
    [self.web loadRequest:[NSURLRequest requestWithURL:url]];
}

如果这不是问题,并且发送到方法的字符串包含www。尝试设置UIWebView的委托,以查看加载请求时是否抛出任何错误。

答案 1 :(得分:0)

设置webview的委托。

试试这个。

NSString *myUrl = @"http://www.YourWebSite.com";
NSURL *webUrl = [NSURL URLWithString:myUrl];
[webObj loadRequest:[NSURLRequest requestWithURL:webUrl]];
相关问题