2010-10-18 140 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更改爲導航視圖控制器,然後我可以基於按鈕點擊推新視圖,謝謝托馬斯也幫助我很多,但我無法弄清楚。