将导航控制器与标签栏控制器相结合

时间:2012-12-23 22:13:25

标签: ios cocoa-touch uinavigationcontroller uitabbarcontroller

正如我在标题中提到的,我想将Navigation Controller添加到已经有Tab Controller的应用程序中。因此,试图让工作人员像这样page。无论如何,有些事情是错的。 UINavigationController正在查看空白页面,即使有视图和一些库。

让我从stracht开始:

在我的AppDelegate中,我正在设置标签栏控制器,如下所示:

@interface MYAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tabBarController;

@end

这是.m文件:

@implementation MYAppDelegate

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    application.applicationSupportsShakeToEdit = YES;
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UINavigationController *viewController1 = [[[MYMainViewController alloc] init] initWithNibName: @"MYMainViewController" bundle:nil];
    UIViewController *viewController2 = [[[MYPageViewController alloc] init] initWithNibName:@"MYPageViewController" bundle:nil];
    UIViewController *viewController3 = [[[MYSearchViewController alloc] init] initWithNibName:@"MYSearchViewController" bundle:nil];
    UIViewController *viewController4 = [[[MYPersonViewController alloc] init] initWithNibName:@"MYPersonViewController" bundle:nil];

    // Initialize tabBarController and add ViewControllers
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects: viewController1, viewController2, 
        viewController3, viewController4, nil];

    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];

    return YES;
}

然后,这是MYMainViewController实现,它是UINavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@", [self navigationController]); // Logging null
}

我的.xib文件有UINavigationController,并且其中有一个视图。尽管如此,当我使用该应用程序时,还有空白页面和无标题导航栏。我做错了什么?

如果我能看到我的视图内容,我想使用后退按钮在两个视图控制器之间导航。

任何帮助或方法对我来说都很棒。

1 个答案:

答案 0 :(得分:4)

尝试从xib中删除导航控制器,因此它只有视图控制器,然后以编程方式初始化导航控制器:

UIViewController *tmpViewController1 = [[[YourViewController alloc] init] initWithNibName:@"YourViewController" bundle:nil];
 UINavigationController *viewController1 =  [[UINavigationController alloc] initWithRootViewController:tmpViewController1];