Loading a UIWebView from a UITableView



我的代码中有一部分有点困惑,我基本上想做的是,一旦你点击UITableView中的谷歌,它就会从一个单独的视图控制器(即UIWebView)加载谷歌。我已经编码了我认为正确的东西,尽管当我点击谷歌时,什么都没发生。我没有收到任何错误,该应用程序运行良好,正如我所说的,一旦你点击选定的字段,它不会导致任何地方&在你说任何话之前,我记得将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是在执行viewDidLoad方法之后设置的。无论如何,在setter中使用loadURL方法(或者一些公共方法来重新加载url并刷新web视图)并不是一个坏主意。

-(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];
}

最新更新