我的应用程序中有一个 ViewController,它有一个在不同视图和标签栏之间切换的方法。为了能够在方法中切换视图,视图从它们的 nib 加载到 viewDidLoad 中。视图的切换基本上可以正常工作,但只有一个视图总是错误的。如果我检查可能是什么问题,我可以在日志中看到该视图的类在加载到 viewDidLoad 后发生更改。一般来说,如果一个对象改变了它的类,就没有什么神秘的魔法,一定会发生一些事情。我想知道加载视图后会发生什么?更准确地说,这里是执行“魔术”的代码:视图在 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]);
}
日志总是显示预期的和正确的类!之后我有了改变视图的方法:
- (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;
}
}
那时日志没有显示预期的结果!这是我的日志的摘录:
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
由于我没有在代码中的任何地方更改视图对象的类,所以我不明白为什么会发生这种情况!有谁知道这可能是什么原因以及如何避免这种情况?只是为了表明我在加载时不会更改类,下面是一个加载视图的示例方法:
- (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;
}