我在我的應用程序中有一個ViewController,它有一個在tabbar不同視圖間切換的方法。爲了能夠切換方法中的視圖,視圖從它們的尖端加載到viewDidLoad中。視圖的切換基本上可以正常工作,但只有一個視圖總是錯誤的。如果我檢查可能是什麼問題,我可以在日誌中看到在viewDidLoad中加載此視圖的類後,它會發生更改。一般來說,如果一個對象改變了它的類,就沒有神祕的魔法,必然會發生。我想知道我的視圖加載後會發生什麼?爲了更精確一點,這裏是一個具有「魔力」的代碼: 的意見在viewDidLoad中ViewController中的視圖類別正在改變?
- (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;
}
1.在loadTasteView中不需要循環查看。只需將對象放在nibViews的索引零處即可。 2.不需要調用viewWillDisappear。這可能會導致問題。 – fibnochi
1.不是。您無法確定視圖總是處於索引零。好的,查看WillDisappear並不是真的有必要。但不叫它也沒有幫助... – usermho