I have a ViewController in my app which has a method that switches between different views od a tabbar. To be able to switch the views in the method the views are loaded in viewDidLoad from their nibs. The switching of the views basically works fine, but just one view always is the wrong one. If I check what could be the problem, I can see in the logs that class of this view changes after it is loaded in viewDidLoad. Generally, there is no mysterious magic if an object changes its class, something must happen. I wonder what might happen after my view is loaded? To be a bit more precise, here is the code that does the "magic": The views are loaded in viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
[self setCurrentView:placeholder];
[self configureView];
wineryView = [self loadWineryView];
DLog(@"- %@", [wineryView class]);
wineView = [self loadWineView];
DLog(@"- %@", [wineView class]);
tasteView = [self loadTasteView];
DLog(@"- %@", [tasteView class]);
}
The log always shows the expected and right class! Afterwards I have the method that changes the views:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
DLog(@"- %d", item.tag);
[[self currentView] removeFromSuperview];
switch ([item tag])
{
case TAB_WINERY:
if (self)
{
[self viewWillDisappear:YES];
[[self view] addSubview:wineryView];
DLog(@"- %@", [wineryView class]);
[self setCurrentView:wineryView];
}
break;
case TAB_WINE:
if (self)
{
[self viewWillDisappear:YES];
[[self view] addSubview:wineView];
DLog(@"- %@", [wineView class]);
[self setCurrentView:wineView];
}
break;
case TAB_TASTE:
if (self)
{
[self viewWillDisappear:YES];
[[self view] addSubview:tasteView];
DLog(@"- %@", [tasteView class]);
[self setCurrentView:tasteView];
}
default:
break;
}
}
At that point log doesn't show the expected result! Here is an excerpt of my log:
2013-01-03 10:20:52.032 Weinerei[454:c07] -[DetailViewController viewDidLoad] [Line 51] - WineryView
2013-01-03 10:20:52.034 Weinerei[454:c07] -[DetailViewController viewDidLoad] [Line 53] - WineView
2013-01-03 10:20:52.036 Weinerei[454:c07] -[DetailViewController viewDidLoad] [Line 55] - TasteView
2013-01-03 10:20:58.946 Weinerei[454:c07] -[DetailViewController tabBar:didSelectItem:] [Line 89] - 0
2013-01-03 10:20:58.948 Weinerei[454:c07] -[DetailViewController tabBar:didSelectItem:] [Line 100] - TasteView
2013-01-03 10:21:00.378 Weinerei[454:c07] -[DetailViewController tabBar:didSelectItem:] [Line 89] - 1
2013-01-03 10:21:00.380 Weinerei[454:c07] -[DetailViewController tabBar:didSelectItem:] [Line 109] - WineView
2013-01-03 10:21:03.306 Weinerei[454:c07] -[DetailViewController tabBar:didSelectItem:] [Line 89] - 2
2013-01-03 10:21:03.308 Weinerei[454:c07] -[DetailViewController tabBar:didSelectItem:] [Line 119] - TasteView
As I don't change the class of the view object anywhere in my code, I do not understand why this happens! Does anyone know what might be a reason for this and how to avoid this? Just to show that I don't change the class while loading, here is an example method for loading the view:
- (UIView *) loadTasteView
{
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"TasteView" owner:self options:nil];
TasteView *tView;
for (id view in nibViews) {
if ([view isKindOfClass:[TasteView class]])
{
tView = (TasteView*) view;
}
}
return tView;
}
Meanwhile I found an awesome solution for my problem which is just incredible: In the header file of one of my view classes I declared a wrong IBOutlet for the view! As I worked with copy&paste, I pasted an IBOutlet of another class in the header. This made me believe the object would change its class!