1

我正在开发一个 iphone 应用程序,其中前四个屏幕是基于导航的

从第五屏开始,设计变成了基于标签栏的设计。在这个基于标签栏的部分中,我们能够导航每个标签上的子视图。

我怎么能实现这个?

我在哪里声明标签栏控制器-在 appDelegate 或其他一些文件中?

4

4 回答 4

1

您可以在 app-delegate 类中添加观察者

[[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(youMethod)        
                                                 name:@"loginSucess" 
                                               object:nil];


- (void)youMethod
{
    UINavigationController *controller = [[[UINavigationController alloc] initWithRootViewController:self.tabBarController] autorelease];


    self.window.rootViewController = controller;    
    [self.window makeKeyAndVisible];
}

对于您想要添加标签栏的操作,您可以像这样调用

    [[NSNotificationCenter defaultCenter] postNotificationName:@"loginSucess" object:nil];
于 2012-05-23T05:39:18.527 回答
0

以编程方式创建 Tabbar 控制器并将 tabbarcontroller 添加到 navigationController

MyFirstViewController *myFirstViewController = [[MyFirstViewController alloc] init];
MySecondViewController *mySecondViewController = [[MySecondViewController alloc] init];

myFirstViewController.array = self.array;


NSArray *array = [[NSArray alloc] initWithObjects:myFirstViewController, mySecondViewController, nil];
UITabBarController *tab = [[UITabBarController alloc] init];
tab.viewControllers = array;

[array release];

UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"first title" image:nil tag:1];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"second title" image:nil tag:2];

myFirstViewController.tabBarItem = item1;
mySecondViewController.tabBarItem = item2;



[self.navigationController pushViewController:tab animated:YES];

使用此代码,这将对您有用..

于 2012-05-23T05:43:14.007 回答
0

我的应用程序中也有类似的问题,最后我想出了推送另一个 UIViewController 并将 UITabBarController 视图添加为子视图。

当你在你的第 4 个屏幕上并且你导航到第 5 个屏幕时,它也是一个 UIViewController。我假设你有 3 个标签。

在这个 UIViewController 的 viewDidLoad

UITabBarController *tabBarController = [[UITabBarController alloc] init];
UIViewController1 *objUIViewController1 = [[UIViewController alloc] initWithNibName:@"UIViewController1" bundle:nil];
UIViewController2 *objUIViewController2 = [[UIViewController alloc] initWithNibName:@"UIViewController2" bundle:nil];
UIViewController3 *objUIViewController3 = [[UIViewController alloc] initWithNibName:@"UIViewController3" bundle:nil];

tabBarController.viewControllers = [NSArray arrayWithObjects:objUIViewController1, objUIViewController2, objUIViewController3, nil];
tabBarController.delegate = self;
[[tabBarController.viewControllers objectAtIndex:0] setTitle:@"Title1"];
[[tabBarController.viewControllers objectAtIndex:1] setTitle:@"title2"];

[self.view addSubview:tabBarController.view];
[UIViewController1 release];
[UIViewController2 release];
[UIViewController3 release];

希望能帮助到你。快乐编码:)

于 2012-05-23T06:13:55.783 回答
0

在应用程序委托中,您声明了前 5 个屏幕(屏幕 1 到 5)假设在屏幕 5 的按钮上您要启动标签栏您执行以下操作:

//Create the tab bar controller
UITabBarController *tabBar = [[UITabBarController alloc] init];
//Create the 5 new view controllers
UIViewController *cont_1 = [[UIViewController alloc] init];

//Add them to array
NSArray *arr = [NSArray arrayWithObjects:cont_1, cont_2, ... , nil]
[tabBar setViewControllers:arr];

//Now push the tab bar controller
[self.navigationController pushViewController:tabBar animated:YES];
于 2012-05-23T05:37:51.483 回答