3

my storyboardI'm having trouble accessing my view controllers under the tab bar controller. Here is what my storyboard looks like:

  1. View Controller A (-> Page View Controller -> View Controller C
  2. View Controller A -> Tab Bar Controller (MyTabBarController.h/.m) -> Navigation Controller (MyNavigationController.h/.m)-> View Controller B (TabViewController.h/.m)
  3. Tab Bar Controller (MyTabBarController.h/.m) -> View Controller D
  4. Tab Bar Controller (MyTabBarController.h/.m) -> View Controller E

From View Controller A I have an IBAction called loginButton that is connected to the Tab Bar Controller, and currently it looks like this:

- (IBAction)loginButton:(id)sender {

    MyNavigationController *localNavigationController;

    UIStoryboard * storyboard = self.storyboard;

    MyTabBarController *tbc = [[MyTabBarController alloc] init];

    NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:1];

    TabViewController *login = [storyboard instantiateViewControllerWithIdentifier: @ "TabViewController"];

    localNavigationController = [[UINavigationController alloc] initWithRootViewController:login];

    localNavigationController.delegate = self;

    [localControllersArray addObject:localNavigationController];


    tbc.viewControllers = localControllersArray;


    tbc.delegate = self;
    tbc.moreNavigationController.delegate = self;


    tbc.selectedIndex = 0;

    [self presentViewController:tbc animated:YES completion:^{

    }];

}

I'm not able to get this displayed correctly. I am getting a bunch of warnings in this piece of code. and it is also not showing the different tab items in the bottom of the Tab Bar, even though I have put images/text on each tab.

So how do I display/access the view controllers inside the Tab Bar Controller correctly? (ie View Controllers C/D/E)?

4

3 回答 3

4

您在问题中显示的情节提要已经包含正确连接的标签栏控制器、导航控制器和登录控制器。因此,您不应该在代码中实例化新的标签栏控制器或导航控制器——它们将在您实例化标签栏控制器时由情节提要实例化。因此,您唯一需要做的就是为情节提要中的标签栏控制器提供一个标识符,然后执行此操作(假设该标识符称为 MyTabBarController):

- (IBAction)loginButton:(id)sender {
    UITabBarController *tbc = [self.storyboard instantiateViewControllerWithIdentifier:@"MyTabBarController"];
    [self presentViewController:tbc animated:YES completion:nil];
}

如果您控制从“登录”按钮到选项卡栏控制器的拖动并选择“模式”,您甚至不需要此代码。这将创建一个模态segue,它将显示标签栏控制器,根本没有代码。

于 2013-12-20T16:29:05.820 回答
0

我建议您使用 Singleton 共享实例来共享来自多个控制器的多个信息。这是一个很好的设计模式供您使用。我正在可可上编写设计模式使用示例(请参阅https://github.com/leverdeterre/DesignPatterns -> 真正的单例)

于 2013-12-20T13:11:26.797 回答
0

如果您只想从 tabBar 控制器中选择另一个选项卡,请使用以下内容:

UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
[tabBar setSelectedIndex:3];

请注意,如果 tabBar 控制器是初始视图控制器,您可以通过applicationDidFinishLaunching方法获取它的实例并将其存储在 AppDelegate 中。然后你就可以像这样访问它:

 MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

记得导入 AppDelegate.h

于 2013-12-20T12:47:12.083 回答