iOS 在数组中存储和加载多个 Web 视图
Posted
技术标签:
【中文标题】iOS 在数组中存储和加载多个 Web 视图【英文标题】:iOS Storing and Loading Multiple web views in and from an array 【发布时间】:2013-09-23 16:01:00 【问题描述】:我的应用曾经使用故事板中的单个 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 视图。
我是在错误的时间加载 URL 还是什么?我不确定为什么我会创建一个 web 视图,但它没有填充任何东西..
似乎正在发生的事情是,当我创建它时,web 视图没有被添加到数组中 - 数组是空的,所以 webView 没有被设置,所以 url 永远不会加载到其中但是我的代码用于将它添加到数组中对我来说似乎没问题..
[webViews insertObject:wv atIndex:index];
【问题讨论】:
让我们continue this discussion in chat 【参考方案1】:我将其从将它们存储在 NSMutableArray 中更改为 NSMutable 字典。这解决了我的问题。
【讨论】:
以上是关于iOS 在数组中存储和加载多个 Web 视图的主要内容,如果未能解决你的问题,请参考以下文章
在 iOS7 中将多个图像加载到单个视图控制器中的方向/分辨率问题