【问题标题】:Loading a UIWebView from a UITableView从 UITableView 加载 UIWebView
【发布时间】:2013-08-01 09:14:25
【问题描述】:

我的代码的一部分让我有点难过,我基本上想要做的是在你点击 UITableView 中的 Google 时加载,然后它会从一个单独的作为 UIWebView 的视图控制器。我已经编写了我认为正确的代码,尽管当我点击 Google 时,什么也没有发生。我没有收到任何错误,应用程序运行良好,正如我所说,一旦你点击选定的字段,它就不会在任何地方引导&在你说任何事情之前,我确实记得将 UIWebView 控制器导入我的第一个视图控制器 .m 文件.

这是我的第一个视图控制器.h

@interface YoMaFifthViewController : UITableViewController

{

NSArray *data, *sites;
} 


@end

这是我的第一个视图控制器.m

- (void)viewDidLoad
{
[super viewDidLoad];

data = [NSArray arrayWithObjects:@"Website", @"Developer", nil];
sites = [NSArray arrayWithObjects:
         @"https://www.google.co.uk/",
         @"https://www.google.co.uk/",
         nil];

`

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

`

 // Configure the cell...

cell.textLabel.text = [data objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;

`

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
YoMaWebsiteViewController *wvc = [[YoMaWebsiteViewController alloc]     initWithNibName:@"YoMaWebsiteViewController" bundle:nil];
wvc.site = [sites objectAtIndex:indexPath.row];
[self.navigationController pushViewController:wvc animated:YES];

}

这是我的 UIWebViews .h

`

@interface YoMaWebsiteViewController : UIViewController
{

IBOutlet UIWebView *webview;

}

@property (retain) NSString *site;


@end

& 这是 UIWebViews .m

`

- (void)viewDidLoad
{
[super viewDidLoad];

NSURL *url = [NSURL URLWithString:self.site];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];

【问题讨论】:

  • 你确认你所有的方法都被命中了吗?另外,视图是否改变了,但 url 是空白的?

标签: uitableview uiwebview


【解决方案1】:

我的猜测是在执行 viewDidLoad 方法后设置 url。无论如何,在你的 setter 中使用 loadURL 方法(或一些公共方法来重新加载 url 并刷新 webview)并不是一个坏主意。

-(void)setSite:(NSString*)s {
    _site = s;

    if(webview) {
        [self loadURL];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if([self.site length] > 0) {
        [self loadURL];
    }
}

-(void)loadURL {
    NSURL *url = [NSURL URLWithString:self.site];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webview loadRequest:request];
}

【讨论】:

  • 我试图在我的代码中实现你所说的,尽管我遇到了很多错误。这是WebView View Controllers .m,请在那里实现。是否需要在我的 WebView 控制器 .h、我的第一个视图控制器 .h 和 .m 中进行任何更改,或者只是 WebView .m
  • 我做了两件事。首先,我将加载 url 代码分解为一个单独的函数。这使得从多个地方进行呼叫变得容易。其次,我在您的站点设置器中调用了 loadURL。这将在属性更改时加载传入的 url。以前有什么问题,现在有什么问题?视图改变了,但是webview没有加载url?
  • 视图根本没有改变,我点击 TableView 上的选定字段,该字段突出显示为蓝色,虽然它没有改变视图,它只是停留在同一页面上 这是一张图片发生了什么:-i.imgur.com/FiUHFdv.png
  • 你能在 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 中设置一个断点,并确保你的代码在一行被选中时命中这个方法吗?
  • 我会这样做,尽管我不知道该怎么做!
猜你喜欢
  • 2011-01-28
  • 1970-01-01
  • 2015-04-28
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 2014-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多