iphone - iOS 在数组中存储和加载多个 Web View

标签 iphone ios objective-c ipad uitableview

我的应用程序过去使用 Storyboard中的单个 UIWebView 来显示用户在线数据。

现在我添加了在应用程序中存储多个帐户的功能,因此我希望能够为每个帐户拥有一个单独的 UIWebView,并显示相应的帐户。

我的应用程序是一个选项卡式应用程序,其中一个选项卡在 TableView 中保存帐户详细信息,另一个选项卡则使用 UIWebView。这是我迄今为止尝试过的拥有多个 WebView 的方法。

在 TableView Controller 中,我的 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 View 的 View Controller

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

似乎正在创建 WebView ,因为我看到了一个可以滚动的白色屏幕,但没有加载任何页面。我假设这也无法隐藏当前未使用的 WebView 。

Web View 应该仅加载之前不存在的页面。如果它已经存在,那么它应该呈现存储在数组中的加载的 Web View 。

我是否在错误的时间加载了 URL 还是其他原因?我不确定为什么我创建了一个 WebView ,但它没有填充任何内容..

似乎发生的情况是,当我创建 web View 时,它没有被添加到数组中 - 数组是空的,因此 webView 没有被设置,因此 url 永远不会加载到其中但是代码我使用将其添加到数组对我来说似乎没问题..

[webViews insertObject:wv atIndex:index];

最佳答案

我将其从将它们存储在 NSMutableArray 中更改为 NSMutable Dictionary。这解决了我的问题。

关于iphone - iOS 在数组中存储和加载多个 Web View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18963796/

相关文章:

iphone - Core Data 是否缓存 transient 属性?

iphone - ios5 上的自动释放替代方案

ios - 在 iOS 中使用 SQLite3 插入语句时出错

iphone - Javascript 文件链接不正确?

javascript - 从 Native iOS 调用 JS 函数

ios - 如何为不同的 iOS 版本定义代码

iphone - 用于调试的 NSLog 语句

iphone - Xcode 4 - 文档 : How to show the Table of Contents?

ios - 在不引入紧耦合的情况下在对象之间传递数据?

ios - Core Data一对多排序