My program is a web browser with MMTabBarView.
Problem with closing the tab closes an invalid element by looking at the log.I have seen that from NSMutableArray * browserViews;
when call [browserViews removeObjectAtIndex: index];
in willCloseTabViewItem
This is the code :
.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);
}
}
And log : The program loads the default Tab google.com:
2015-03-26 17:51:57.634 ERClient[89171:26065490] didSelectTabViewItem Index 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 open in a new Tab:
2015-03-26 17:53:51.994 ERClient[89171:26065490] createWebViewWithRequest!
2015-03-26 17:53:51.995 ERClient[89171:26065490] didSelectTabViewItem Index 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! - I do not understand why this happens 3 times instead of 2
Focus on Tab 2 with index 1, I change the focus to the first Tab index 0, and then return back:
2015-03-26 17:57:56.384 ERClient[89171:26065490] didSelectTabViewItem Index 0
2015-03-26 17:57:56.477 ERClient[89171:26065490] Index change focus tab 0
2015-03-26 17:57:58.206 ERClient[89171:26065490] didSelectTabViewItem Index 1
2015-03-26 17:57:58.207 ERClient[89171:26065490] Index change focus tab 1
Now I close Tab 2 with index 1:
2015-03-26 17:58:55.691 ERClient[89171:26065490] willCloseTabViewItem Index 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 Index 0
2015-03-26 17:58:55.693 ERClient[89171:26065490] Index change focus tab 0
It turns out that open Tab 1 with a valid title but the content remained on the Tab 2 :
And if I open another Tab and there will also be displayed gmail Tell me how to fix it or where to read why this is happening
Problem was solved by itself.
The problem was due to the fact that I have a little experience in programming.
Now I will describe why not work.
When you delete an object from NSMutableArray still remained WebView object with the loaded page. It was necessary to add [browserView removeFromSuperview];
The code is as follows:
- (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];
}
}
Maybe someone will be useful in the future to create a Web Browser with Tab Bar