我的应用程序曾经使用故事板中的单个UIWebView来显示用户的在线数据。
现在,我添加了在应用程序中存储多个帐户的功能,因此我希望能够为每个帐户创建一个单独的UIWebView,并显示相应的帐户。
我的应用程序是一个选项卡式应用程序,其中一个选项卡在表视图中保存帐户详细信息,另一个选项卡具有UIWebView。这就是我到目前为止所尝试的具有多个web视图的方法。
在表视图控制器中,我的didSelectRowAtIndexPath如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Generate the url (please note I've taken this code out)
...
//Get the view controller that will hold the web view
NSArray *viewControllers = self.tabBarController.viewControllers;
ViewerFirstViewController *viewerViewController = [viewControllers objectAtIndex:0];
//Look for the web view in an array of web views
[viewerViewController getWebView:indexPath.row];
[viewerViewController setViewerUrl:viewerURL];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//Move to the tab that will hold the web view (bonus question - how can I animate this move?
[self.tabBarController setSelectedIndex:0];
}
现在,对于将保存web视图的视图控制器
-(void)getWebView:(int)index
{
if ([webViews objectAtIndex:index] == NULL) {
[self createWebView:index];
[self setDidExist:false];
[self setWebView:[webViews objectAtIndex:index]];
} else {
[self setDidExist:true];
[self setWebView:[webViews objectAtIndex:index]];
}
}
-(void)createWebView(int)index
{
float width = self.view.bounds.size.width;
float height = self.view.bounds.size.height;
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, width, height)];
wv.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
wv.scalesPageToFit = YES;
wv.delegate = self;
[self.view addSubview:wv];
[webViews insertObject:wv atIndex:index];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewDidLoad];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:viewerUrl];
if (didExist) {
[webView loadRequest:requestObj];
[webView setHidden:false];
} else {
[webView loadRequest:requestObj];
}
}
当我看到一个可以滚动的白色屏幕时,似乎正在创建web视图,但没有加载任何页面。我想这也无法隐藏当前未使用的web视图。
web视图应该只加载以前不存在的页面。如果它已经存在,那么它应该显示存储在数组中的加载的web视图。
我是不是在错误的时间加载URL?我不确定为什么我会创建一个网络视图,但它没有任何内容。。
似乎发生的情况是,当我创建web视图时,它没有被添加到数组中-数组是空的,因此没有设置webView,因此url从未加载到其中。然而,我用来将其添加到数组的代码对我来说似乎没问题。。
[webViews insertObject:wv atIndex:index];