我有一个使用UITableViewController的应用程序,它调用内部有UIWebView的UIViewController。我有一切工作正常,除了当我点击一个表格单元格,它带我到UIViewController并打开相应的weburl里面,但当我回到UITableViewController它不会忽略网站。它抵消了网站,所以我知道我回到了tableview,但网站仍然可见。我错过了什么?
WebViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)viewDidAppear:(BOOL)animated {
webView2.delegate = self;
webView2.backgroundColor = [UIColor clearColor];
NSString *urlAddress = self.magURL;
webURL = [NSURL URLWithString:urlAddress];
requestObj = [NSURLRequest requestWithURL:webURL];
[webView2 loadRequest:requestObj];
}
-(void)viewDidDisappear:(BOOL)animated
{
webView2.delegate = nil;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
TableViewController.m - (void)viewDidLoad { [super viewDidLoad];
magazineArray = [[NSMutableArray alloc] init];
[magazineArray addObject:@"Joyce Meyer"];
[magazineArray addObject:@"Perry Stone"];
[magazineArray addObject:@"Robb Thompson"];
[magazineArray addObject:@"R.W. Schambach"];
[magazineArray addObject:@"T.D. Jakes"];
[magazineArray addObject:@"Reinhard Bonnke"];
[magazineArray addObject:@"Pilot"];
self.navigationItem.title = @"Magazines";
magazineURL = [[NSMutableArray alloc] init];
[magazineURL addObject:@"http://mysolutionsmagazine.com/solutions/2013/magazine"];
[magazineURL addObject:@"http://google.com"];
[magazineURL addObject:@"http://smithmediagroup.com"];
[magazineURL addObject:@"http://smgvoices.com"];
[magazineURL addObject:@"http://facebook.com/smithmediagroup"];
[magazineURL addObject:@"http://twitter.com/smgvoices"];
[magazineURL addObject:@"http://youtube.com/smgvoices"];
MagazineURLViewController *urlController = [[MagazineURLViewController alloc] init];
urlController.urlHolder = newURLHolder;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
newURLHolder = [magazineURL objectAtIndex:indexPath.row];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(50, 100, 690, 750)];
NSURL *webURL2 = [NSURL URLWithString:newURLHolder];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:webURL2];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"Cell2"]){
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
newURLHolder = magazineURL[indexPath.row];
[[segue destinationViewController]setMagURL:newURLHolder];
}
}
答案 0 :(得分:1)
您将webviewcontriller的视图添加到tableviewcontroller的视图作为子视图。它可能看起来没有任何问题,我认为这是苹果的严格禁忌(iPad是一个例外,因为它有你可以使用的splitviewcontroller)。
当您将其添加为子视图时,将不会调用默认的viewcontroller方法(viewwill / didappear等)。
将其作为新的视图控制器推送,或以模态方式呈现它,当您弹出/关闭webview控制器时,一切都将按预期工作。
如果你不想推送或呈现模态,只需在tableviewController中使用webview,实现webView的委托方法,将其添加为子视图。
答案 1 :(得分:0)
而不是将webview添加为子视图,而是推送webview:
[self.navigationController pushViewController:webview animated:YES];
当你想要关闭webview时:
[self.parentViewController.navigationController popViewControllerAnimated:YES];]
答案 2 :(得分:0)
目前看起来你永远不会调用你在单元格选择上创建UIWebView的webViewController并将其添加到当前视图。所以我的问题是你有没有说过removeFormSuperview?
答案 3 :(得分:0)
看起来您正在使用Storyboards。问题似乎是
中不必要的代码 tableView:didSelectRowAtIndexPath:
您不必将网络视图添加到当前视图,您需要在@"Cell2"
视图控制器中包含该代码。
看起来你几乎就在那里,你将URL传递给prepareForSegue
你应该有另一个视图控制器来读取magURL属性并显示该网页。
在表视图控制器中更像这样:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
newURLHolder = [magazineURL objectAtIndex:indexPath.row];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"Cell2"]){
[[segue destinationViewController] setMagURL:newURLHolder];
}
}
然后在新的视图控制器中
- (void)viewDidLoad
{
// if webview is created in the storyboard you won't need to `init` it here. Just use [self webView]
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(50, 100, 690, 750)];
NSURL *webURL2 = [NSURL URLWithString:[self magURL]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:webURL2];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
}