导航控制器加载两次,如何解决?

时间:2012-03-22 10:38:25

标签: ios xcode4 uinavigationcontroller uitabbarcontroller uisegmentedcontrol

我的项目以编程方式包含导航控制器和分段控件(带有单独的视图控制器:segmentManagingViewController),现在我在IB中添加了一个标签栏,同时调用标签栏控制器和导航控制器,segmentManagingViewController视图加载两次..都在标签中bar item1和第一段我调用了segmentManagingViewController视图....

这是我的应用程序的屏幕截图

screen shot

以下是 应用程序didFinishLaunchingWithOptions方法...请帮我解决这个问题......

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    databaseName = @"breadworks.sql";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];


    [self checkAndCreateDatabase];
    [self readBreadsFromDatabase];
    [self categoryFromDatabase];
    SegmentManagingViewController * segmentManagingViewController = [[SegmentManagingViewController alloc] init];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:segmentManagingViewController];
    [segmentManagingViewController release];

    [self.window addSubview:tabBarController.view];
    [window addSubview: navigationController.view];
    [window makeKeyAndVisible];


    return YES;
}

2 个答案:

答案 0 :(得分:1)

你的代码听起来很奇怪。

首先,因为您使用UITabBarController将其设置为窗口的rootViewController

然后,将UINavigationController设置为标签栏控制器的子控制器。

最后,正如您所做的那样,将rootViewController的{​​{1}}设置为UINavigationController

现在,因为我更喜欢在没有xib的情况下执行此操作,所以您可以执行以下操作。

segmentManagingViewController

如果不使用ARC,请注意内存管理!!

希望它有所帮助。

答案 1 :(得分:1)

我在委托中声明了导航控制器和视图控制器(用作NSArray的对象),并为视图控制器创建了initWithNibName构造函数(定义了TabBarItems的标题,图像和其他属性)..这里是更新的代码块.. < / p>

UIViewController *viewController1 = [[AtoZSecondviewController alloc] initWithNibName:@"AtoZSecondviewController" bundle:nil];
UIViewController *viewController2 = [[CategorySecondViewController alloc] initWithNibName:@"CategorySecondViewController" bundle:nil];

  tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController,viewController1 ,viewController2, nil];  

以下是ViewControllers中的initWithNibName的定义

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self) {
        self.title = NSLocalizedString(@"Catogaries", @"Catogaries");
        self.tabBarItem.image = [UIImage imageNamed:@"TodaysChoice"];
    }
    return self;
}