ViewController中的View类正在改变?

时间:2013-01-03 09:29:25

标签: ios uiview uitabbar

我的app中有一个ViewController,它有一个方法可以在tabbar的不同视图之间切换。为了能够在方法中切换视图,视图将从其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;
}

1 个答案:

答案 0 :(得分:0)

与此同时,我发现了一个非常棒的解决方案,这个问题令人难以置信:在我的一个视图类的头文件中,我为视图声明了一个错误的IBOutlet!当我使用copy& paste时,我在标题中粘贴了另一个类的IBOutlet。这让我相信这个对象会改变它的阶级!