0

我有一個方法,它應該初始化一個導航控制器並加載一個側面菜單的視圖控制器。這一切都發生在未連接到任何其他導航控制器的視圖控制器內。[UINavigationController setView]和[UINavigationController setViewControllers]之間的區別

- (void)showLeftNC 
{ 
    if (leftNavCon == nil) 
    { 
     leftNavCon = [[UINavigationController alloc] init]; 
    } 

    [leftNavCon setViewControllers:@[lmvc] animated:NO]; 

    //[leftNavCon setView:lmvc.view]; 

    [leftNavCon.view setFrame:lmvc.view.frame]; 

    [self.view addSubview:leftNavCon.view]; 

    [self showCenterViewWithShadow:YES withOffset:-2]; 

    [self.view sendSubviewToBack:leftNavCon.view]; 
} 

leftNavCon是導航控制器

lmvc是主視圖控制器

它不會以這種方式工作,既當我打電話initWithRootViewController:lmvc

它只有工作當我使用評論[leftNavCon setView:lmvc.view]。但即使如此,我仍然無法讓導航控制器推送任何其他視圖控制器。

請幫忙。

+0

'[leftNavCon小號etView:lmvc.view]'表示您將UINavigationController的視圖設置爲lmvc的視圖。我猜在運行代碼之後推送的viewController'view將在UINavigationController的視圖之下。你確定'[leftNavCon setViewControllers:@ [lmvc] animated:NO];'不工作?我總是這樣做。 – Ryan

+0

它現在正在工作,但我使用視圖控制器作爲導航控制器的容器,因爲它無法單獨調用addSubView –

回答

0

沒關係,我摸索出一些東西,我用lmvc作爲含觀點,初始化導航控制器和所有的視圖控制器我需要它裏面的lmvc initWithNibName

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 

     if (testViewController == nil) 
     { 
      testViewController = [[UIViewController alloc] init]; 
     } 

     if (navCon == nil) 
     { 
      navCon = [[UINavigationController alloc] initWithRootViewController: testViewController]; 
     } 

     view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 748)]; 

     [self setView:navCon.view]; 

     [testViewController.view addSubview:table]; 
     [testViewController.view addSubview:button]; 
     [testViewController.view addSubview:anyControlYouNeed]; 
} 

然後

稍後

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if (tableTwo == nil) 
    { 
     tabl = [[UITableView alloc] initWithFrame:table.frame]; 
    } 

    if (testViewControllerTwo == nil) 
    { 
     testViewControllerTwo = [[UIViewController alloc] init]; 
    } 

    tableTwo.delegate = self; 
    tableTwo.dataSource = self; 
    [testViewControllerTwo setView:tableTwo]; 

    [navCon pushViewController: testViewControllerTwo animated:YES]; 
} 

就像一個魅力

相關問題