0

我在xcode 4.2中创建了一个基于视图的项目,我想实现多级导航以导航到不同的视图。我的第一个视图类是ViewController,我显示一个表。在点击一行时,我会去下一个有tabbar的班级。在这个我有3个选项卡。第一个选项卡使用的是BuyerViewController类。它有4个按钮。问题是,当我点击按钮时,它不会推下一个课程。iphone中的多级导航xcode 4.2

ViewController.m

- (void)viewDidLoad 
{ 
[email protected]"Select County"; 
    tView = [[UITableView alloc] init]; //tView is my table view object. 
    tView.frame=CGRectMake(0, 20, 320, 460); 
    [tView setDelegate:self]; 
    [tView setDataSource:self]; 
    [self.view addSubview:tView]; 
} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return [countyArray count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
cell.textLabel.text = [countyArray objectAtIndex:indexPath.row]; 
return cell; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
//This works fine and pushes to my next class to show the tabs. 

ShowOptionInTab *showTabbar = [[ShowOptionInTab alloc] initWithNibName:@"ShowOptionInTab" bundle:nil]; 
[self.navigationController pushViewController:showTabbar animated:YES]; 
[showTabbar release]; 
} 

ShowOptionInTab.m

-(void)loadView { 
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
contentView.backgroundColor = [UIColor whiteColor]; 
self.view = contentView; 
[contentView release]; 

BuyerViewController *buyerController = [[BuyerViewController alloc] init ]; 

SellerViewController *sellerController = [[SellerViewController alloc] init]; 

LenderViewController *lenderController = [[LenderViewController alloc] init]; 

[email protected]"Buyer"; 
sellerController.title = @"Seller"; 
lenderController.title = @"Lender"; 

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

tabbarController.view.frame = CGRectMake(0, 0, 320, 460); 
[tabbarController setViewControllers:[NSArray arrayWithObjects:buyerController, sellerController,lenderController, nil]]; 

[buyerController release]; 
[sellerController release]; 
[lenderController release]; 
[self.view addSubview:tabbarController.view];  
} 

BuyerViewController.m

-(void)viewWillAppear:(BOOL)animated 
{ 
UIButton *quickEstimateButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

    [quickEstimateButton addTarget:self action:@selector(getTagOfPressedButton:) forControlEvents:UIControlEventTouchUpInside]; 
[quickEstimateButton setTitle:@"QUICK ESTIMATE" forState:UIControlStateNormal]; 
quickEstimateButton.tag=1; 
quickEstimateButton.frame = CGRectMake(10, 50, 150, 40.0); 
[self.view addSubview:quickEstimateButton]; 

//more 3 buttons are added in same way 
} 
-(void) getTagOfPressedButton:(id)sender { 

UIButton *getTagOfButton = (UIButton *)sender; 
int buttonPressedTag = getTagOfButton.tag; 
NSLog(@"TAG==========%d",buttonPressedTag); 
if(buttonPressedTag==1) 
{ 
    NSLog(@"quick estimate pressed"); 
    QuickEstimateViewController *q = [[QuickEstimateViewController alloc] init]; 
    NSLog(@"nav====%@",self.navigationController); //This returns null. 
    [self.navigationController pushViewController:q animated:YES]; 
} 
} 

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
     ViewController *overviewViewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil]; 
    UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:overviewViewController]; 
    navigation.navigationBar.tintColor = [UIColor blackColor]; 
    [overviewViewController release]; 

    [self.window addSubview:[navigation view]]; 
    [self.window makeKeyAndVisible]; 
} 

为什么QuickEstimateViewController不显示?

回答

0

因为你在ShowOptionInTab中定义了一个tabBarController的新对象和它的视图作为子视图,然后还定义了一个新的对象BuyerViewController,它试图推送QuickEstimateViewController的navigationController将是零..我认为它会工作,如果你的代码看起来像这样..

BuyerViewController *buyerController = [[BuyerViewController alloc] init ]; 
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:buyerController]; 

SellerViewController *sellerController = [[SellerViewController alloc] init]; 

LenderViewController *lenderController = [[LenderViewController alloc] init]; 

[email protected]"Buyer"; 
sellerController.title = @"Seller"; 
lenderController.title = @"Lender"; 

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

tabbarController.view.frame = CGRectMake(0, 0, 320, 460); 
[tabbarController setViewControllers:[NSArray arrayWithObjects:nav, sellerController,lenderController, nil]];