【问题标题】:Tabbar controller load empty xib标签栏控制器加载空的xib
【发布时间】:2012-09-06 11:22:37
【问题描述】:

我正在尝试构建一个以 3 个按钮开头的主视图的应用程序,然后当用户按下这些按钮中的任何按钮时,标签栏视图将与选定的标签栏项目一起出现。

我的问题在这里,当标签栏视图应该出现时......它显示为空!

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.

    MainMenuViewController *mainMenuViewController = [[[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController" bundle:nil] autorelease];
    self.navigationController = [[[UINavigationController alloc] initWithRootViewController:mainMenuViewController] autorelease];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

//主菜单视图中的按钮操作 -

 (IBAction)button1Action:(id)sender {
        TabbarViewController *tempView = [[TabbarViewController alloc] initWithNibName:@"TabbarViewController" bundle:nil];
        [self.navigationController pushViewController:tempView animated:YES];
        [tempView release];
    }

【问题讨论】:

    标签: iphone xcode4 uinavigationcontroller tabbarcontroller


    【解决方案1】:

    您必须设置 TabbarViewController 的 viewControllers 属性(当然,如果它是 UITabBarController 的超类)。在 TabbarViewController 的 init 方法中创建 3 个 viewControllers,将它们添加到数组中并将其设置为 viewControllers 属性,如下所示:

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            NSLog(@"%@",[[self class] superclass]);
    
            UIViewController *yourFirstViewController = [[UIViewController alloc] init];
            UIViewController *yourSecondViewController = [[UIViewController alloc] init];
            UIViewController *yourThirdViewController = [[UIViewController alloc] init];
    
            yourFirstViewController.title = @"First";
            yourSecondViewController.title = @"Second";
            yourThirdViewController.title = @"Third";
    
            NSArray *threeViewControllers = [[NSArray alloc]initWithObjects:yourFirstViewController, yourSecondViewController, yourThirdViewController, nil];
    
            self.viewControllers = threeViewControllers;
    
            // Custom initialization
        }
        return self;
    }
    

    【讨论】: