0

我正在关注 Iphone UI 选项卡控制器教程,我基本上模仿了底部 链接文本中的代码

// Create a temporary window object to assign to the window property 
    UIWindow *tempWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]
 bounds]];
    // Make the window background red so that you can see that the window has been added
    tempWindow.backgroundColor = [UIColor grayColor];
    self.window = tempWindow;
    [tempWindow release];


    // add the tab bar controller
    UITabBarController *tabBarController;
    tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];


    PhoneContactsView *phoneContactsView = [[[PhoneContactsView alloc] init] autorelease];
    OtherContactsView *otherContactsView = [[[OtherContactsView alloc] init] autorelease];

    //Add all the view to the tabBarView
    tabBarController.viewControllers = [NSArray arrayWithObjects:otherContactsView,phoneContactsView, nil];

    //Add all the button's to the bar

    UITabBarItem *otherContactsTabBarItem = 
    [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:1];       
    otherContactsBarItem.title = @"My Contacts";
    otherContactsView.tabBarItem = otherContactsBarItem;    
    [otherContactsBarItem release];

    UITabBarItem *phoneContactsBarItem =
    [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1];       
    phoneContactsView.tabBarItem = phoneContactsBarItem;    
    [phoneContactsBarItem release];

    tabBarController.selectedViewController
    = [tabBarController.viewControllers objectAtIndex:0];

    [window addSubview:tabBarController.view];
    [tabBarController release];
    [window makeKeyAndVisible];

OtherContacts 和 PhoneContacts 只是默认的空视图,我只更改了背景以确保它们正在加载。当我运行这个时,第一个视图加载,但我无法单击选项卡栏在视图之间切换我错过了什么吗?

4

2 回答 2

1

发现BUG:

[window addSubview:tabBarController.view];
[tabBarController release]; <------------- I am releasing the tabBarController while still in use 
[window makeKeyAndVisible];

这就是标签栏控制器没有响应的原因。-丹尼尔

于 2009-07-08T05:28:06.450 回答
0

如果 OtherContacts 和 PhoneContacts 是“UIView”类而不是 UIViewController 类,这将解释问题 - 选项卡栏控制器需要 UIViewController 引用。

于 2009-07-08T03:38:52.783 回答