2010-10-18 143 views
1

我的应用程序基于tabbar控制器 现在在我的默认视图中我正在显示一个viewController并让它说它有按钮A,当用户按A它应该加载我的tableviewController但没有发生什么?从iphone上的tabbarview控制器加载导航控制器

-(IBAction)promo:(id)sender 
{ 
    aRoot= [[tableViewController alloc] initWithNibName:@"tableViewController" bundle:[NSBundle mainBundle]]; 
    [self.navigationController pushViewController:aRoot animated:YES]; 

} 

但它没有加载任何没有错误甚至???

///////////更新

我做了这个

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  





Promo *aPromo = [[Promo alloc] initWithNibName:nil bundle:nil];//button A is deifned on this VC 
// then... 
aNav = [[UINavigationController alloc] initWithRootViewController:aPromo]; 
// [pageOne release]; 

和promoviewController

-(IBAction)promo:(id)sender 
{atab= [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:nil]; 

//TableViewController *atab1 = [[TableViewController alloc]initWithNibName:@"TableViewController" bundle:[NSBundle mainBundle]]; 


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



} 
+0

你在你的标签中使用NavigationController吗?如果你没有一个,你应该加一个。您可以将IB中的navigationController拖动到您的项目中。 – 2010-10-18 08:12:07

回答

0

您需要一个navigationController才能推送viewController。

0

你不能一个视图 - 控制添加到中的UITabBar一样如果你想使用导航控制器。

,你做你的标签栏控制器,你必须这样做:

MyFirstTabViewController *pageOne = [[MyFirstTabeViewController alloc] initWithNibName:nil bundle:nil]; 
// then... 
UINavigationController *ncOne = [[UINavigationController alloc] initWithRootViewController:pageOne]; 
[pageOne release]; 

再加入ncOne到标签栏,而不是视图控制器。 :)然后你的问题代码应该工作(因为你正确地声明aRoot在头)。

编辑 再次启动......选择view based application调用它TabBarTest

右键单击类并添加三个新类。它们需要是UIViewController UITableViewController的子类。可以说他们被称为RootViewOneRootViewTwoSecondaryViewController

然后打开TabBarTestViewController.m

取消对viewDidLoad方法。

你必须现在把这个代码在方法:

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

NSMutableArray *viewControllers = [NSMutableArray array]; 

RootViewOne *vc1 = [[RootViewOne alloc] initWithNibName:nil bundle:nil]; 
UINavigationController *nc1 = [[UINavigationController alloc] initWithRootViewController:vc1]; 
nc1.view.backgroundColor = [UIColor redColor]; 
[viewControllers addObject:nc1]; 
[vc1 release]; 

RootViewTwo *vc2 = [[RootViewTwo alloc] initWithNibName:nil bundle:nil]; 
UINavigationController *nc2 = [[UINavigationController alloc] initWithRootViewController:vc2]; 
nc2.view.backgroundColor = [UIColor blueColor]; 
[viewControllers addObject:nc2]; 
[vc2 release]; 

[tbc setViewControllers:viewControllers animated:YES]; 

[self presentModalViewController:tbc animated:YES]; 

现在打开RootViewOne.m和viewDidLoad把这个:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setTitle:@"Click to move through stack" forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(moveToNextView:) forEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:button]; 

现在,你将需要一个自定义的方法:

-(void)moveToNextView:(id)selector { 
    SecondaryViewController *page = [[SecondaryViewController alloc] initWithNibName:nil bundle:nil]; 
    page.title = @"Next Page"; 
    page.view.backgroundColor = [UIColor greenColor]; 
    [self.navigationController pushViewController:page animated:YES]; 
    [page release]; 
} 

这只是基本的,但你应该了解kinad进程y你需要经历。我直接在浏览器中输入这个内容,这样可能会出现拼写错误......小心你是否有任何错误或警告。希望这可以帮助你完成你的项目。

+0

我应该在哪里放这个代码?在appdidfinsihlaunch ?? – prajakta 2010-10-18 09:09:26

+0

我这样做 - (BOOL)应用:(UIApplication的*)应用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions { 促销* aPromo = [[促销的alloc] initWithNibName:无束:无]; //然后... aNav = [[UINavigationController alloc] initWithRootViewController:aPromo]; // [pageOne release]; – prajakta 2010-10-18 09:18:06

+0

呃,那不正确....你使用IB吗?不幸的是,我不使用IB,我的解决方案是编程式的。 – 2010-10-18 09:39:28

0

最后,我解决了这个问题,我将VC更改为导航视图控制器,然后我可以基于按钮点击推新视图,谢谢托马斯也帮助我很多,但我无法弄清楚。