0

我已经来到我的代码的一部分,我有点难过,我基本上想做的是在你点击 UITableView 中的谷歌时加载,然后它会从一个单独的视图控制器加载谷歌是一个 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];
4

1 回答 1

0

My guess is the url is getting set after the viewDidLoad method is being executed. Regardless, it's not a bad idea to have the loadURL method in your setter (or some public method to re-load the url and refresh the 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];
}
于 2013-08-01T01:14:25.353 回答