NSMutableArray removeObjectAtIndex和NSTabView

时间:2015-03-26 16:58:21

标签: objective-c macos webview nsmutablearray nstabview

我的程序是带有MMTabBarView的Web浏览器。 关闭标签的问题通过查看日志来关闭无效元素。我在willCloseTabViewItem中调用NSMutableArray * browserViews;时看到[browserViews removeObjectAtIndex: index]; 这是代码: .H

{
    IBOutlet NSTabView *tabView;
    IBOutlet MMTabBarView *tabBarControl;
    IBOutlet NSSearchField *searchField;
    IBOutlet NSView *mainView;
    NSMutableArray *browserViews;
}
- (MMTabBarView *)tabBarControl;
- (ERBrowser *)addWebView:(NSURL *)url;
- (void)addDefaultTabs;

的.m

- (id)initWithWindow:(NSWindow *)window
        {
            if (self = [super initWithWindow:window])
            {
                browserViews = [[NSMutableArray alloc] init];
            }
            return self;
        }

- (void)windowDidLoad{
            [super windowDidLoad];
           for (NSTabViewItem *item in [tabView tabViewItems])
                [tabView removeTabViewItem:item];
            [tabView setAutoresizesSubviews:true];
            [searchField setDelegate:self];
            [tabBarControl setDelegate:self];   
        }

- (ERBrowser *)addWebView:(NSURL *)url{
            for (NSView *view in browserViews)
                [view setHidden:false];
            ERTabModel *tabModel = [[ERTabModel alloc] init];
            NSTabViewItem *tabViewItem = [[NSTabViewItem alloc] initWithIdentifier:tabModel];
            [tabView addTabViewItem:tabViewItem];
            [tabView selectTabViewItem:tabViewItem];
            ERBrowser *browserView = [[ERBrowser alloc] initWithFrame:mainView.frame];
            [browserView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
            [mainView addSubview:browserView];
            [browserViews addObject:browserView];
            [browserView setUIDelegate:self];
            [browserView setFrameLoadDelegate:self];
            NSLog(@"%@", browserViews);
            if (url)
                [[browserView mainFrame] loadRequest:[NSURLRequest requestWithURL:url]];
            return browserView;
        }

- (void)addDefaultTabs {
            [self addWebView:[NSURL URLWithString:@"http://google.com"]];
        }

- (MMTabBarView *)tabBarControl {
            return tabBarControl;
        }

// Method called when webView javascript requests new window with request
- (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
        {
            NSLog(@"createWebViewWithRequest!");
            return [self addWebView:[request URL]];
        }
// MMTabBarVievDelegate
- (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem {
        NSUInteger index = [aTabView indexOfTabViewItem:tabViewItem];
        NSLog(@"didSelectTabViewItem Index %ld", (long)index);

        if ([browserViews count] > index)
        {
            for (NSView *view in browserViews)
                [view setHidden:true];
            ERBrowser *browserView = browserViews[index];
            [browserView setHidden:false];
            NSLog(@"Index change focus tab %ld", (long)index);

            [searchField setStringValue:@""];
            [tabViewItem setLabel:[browserView stringByEvaluatingJavaScriptFromString:@"document.title"]];
        }

    }

- (void)tabView:(NSTabView *)aTabView willCloseTabViewItem:(NSTabViewItem *)tabViewItem
    {

        NSUInteger index = [aTabView indexOfTabViewItem:[tabView selectedTabViewItem]];
            NSLog(@"willCloseTabViewItem Index %ld", (long)index);
       if ([browserViews count] > index)
       {
            [browserViews removeObjectAtIndex:index];
            NSLog(@"%@" @"%ld", browserViews, (long)index);
       }
    }

并记录: 该程序加载默认的标签google.com:

  

2015-03-26 17:51:57.634 ERClient [89171:26065490] didSelectTabViewItem   指数0

     

2015-03-26 17:51:59.030 ERClient [89171:26065490](ERBrowser:   0x600000124060)

     

2015-03-26 17:52:02.278 ERClient [89171:26065490] didReceiveTitle!

Gmail在新标签页中打开:

  

2015-03-26 17:53:51.994 ERClient [89171:26065490]   createWebViewWithRequest!

     

2015-03-26 17:53:51.995 ERClient [89171:26065490] didSelectTabViewItem   指数1

     

2015-03-26 17:53:52.035 ERClient [89171:26065490](ERBrowser:   0x600000124060,ERBrowser:0x600000121cc0)

     

2015-03-26 17:53:53.987 ERClient [89171:26065490] didReceiveTitle!

     

2015-03-26 17:53:56.451 ERClient [89171:26065490] didReceiveTitle!

     

2015-03-26 17:53:59.003 ERClient [89171:26065490] didReceiveTitle! -   我不明白为什么会发生这种情况3次而不是2次

关注带有索引1的Tab 2,我将焦点更改为第一个Tab索引0,然后返回:

  

2015-03-26 17:57:56.384 ERClient [89171:26065490] didSelectTabViewItem   指数0

     

2015-03-26 17:57:56.477 ERClient [89171:26065490]指数变化焦点   标签0

     

2015-03-26 17:57:58.206 ERClient [89171:26065490] didSelectTabViewItem   指数1

     

2015-03-26 17:57:58.207 ERClient [89171:26065490]指数变化焦点   标签1

现在我关闭带有索引1的Tab 2:

  

2015-03-26 17:58:55.691 ERClient [89171:26065490] willCloseTabViewItem   指数1

     

2015-03-26 17:58:55.691 ERClient [89171:26065490](ERBrowser:   0x600000124060)1

     

2015-03-26 17:58:55.692 ERClient [89171:26065490] didSelectTabViewItem   指数0

     

2015-03-26 17:58:55.693 ERClient [89171:26065490]指数变更焦点   标签0

事实证明,使用有效标题打开标签1,但内容仍保留在标签2上: enter image description here

如果我打开另一个标签页,那么还会显示gmail 告诉我如何修复它或在哪里阅读为什么会发生这种情况

2 个答案:

答案 0 :(得分:0)

问题本身已经解决了。 问题是由于我在编程方面有一点经验。 现在我将描述为什么不工作。 从NSMutableArray中删除对象时,仍然保留带有加载页面的WebView对象。有必要添加[browserView removeFromSuperview];

代码如下:

- (void)tabView:(NSTabView *)aTabView willCloseTabViewItem:(NSTabViewItem *)tabViewItem
{
    NSUInteger index = [aTabView indexOfTabViewItem:[tabView selectedTabViewItem]];
   if ([browserViews count] > index)
   {
       ERBrowser *browserView = browserViews[index];
       [browserViews removeObjectAtIndex:index];
       [browserView removeFromSuperview];
   }
}

将来创建带有标签栏的Web浏览器

可能会有用

答案 1 :(得分:0)

此外,您应该根据文档调用super方法:

/* Implemented methods from NSTabViewDelegate. These require a call to super if overriden by a subclass. */
- (void)tabView:(NSTabView *)tabView willSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem NS_REQUIRES_SUPER;
- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem NS_REQUIRES_SUPER;
- (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem NS_REQUIRES_SUPER;