如何以编程方式在视图中嵌入导航控制器?

问题描述:

我有一个视图控制器,这是我的HomeViewController,我有他们之间的模态赛格。如何以编程方式在视图中嵌入导航控制器?

这是HomeViewController:

import "HomePageViewController.h" 
#import "CreatePageViewController.h" 
#import "StackTableViewController.h" 
#import "PopUpView.h" 


@interface HomePageViewController() 

@property (nonatomic, strong) IBOutlet UIButton *toggleButton; 
@property (nonatomic, strong) CreatePageViewController *modalTest; 
@property (nonatomic, strong) PopUpView *popup; 

@end 

@implementation HomePageViewController 

-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
} 

-(void)viewDidLoad { 
    [super viewDidLoad]; 

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:Nil]; 
    _modalTest = [storyboard instantiateViewControllerWithIdentifier:@"ModalTest"]; 

    [_toggleButton addTarget:self action:@selector(go) forControlEvents:UIControlEventTouchUpInside]; 

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hide) name:@"HideAFPopup" object:nil]; 
} 

-(void)go { 

    _popup = [PopUpView popupWithView:_modalTest.view]; 
    [_popup show]; 
} 

-(void)hide { 

    [_popup hide]; 
} 
- (IBAction)pushToNextViewController:(id)sender { 

    StackTableViewController *vc = [[StackTableViewController alloc]init]; 
    [self presentModalViewController:vc animated:YES]; 
} 


-(void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

@end 

我想一个导航控制器添加到StackTableViewController ......它只是一个表视图,我希望它有一个导航控制器,我应该怎么办呢?

此外,为什么xcode告诉我我的模态方法presentModalViewController已弃用?

TNX

创建的UINavigationController一个新实例,你StackTableViewControllerrootViewController。然后呈现导航控制器:

- (IBAction)pushToNextViewController:(id)sender { 

    StackTableViewController *vc = [[StackTableViewController alloc]init]; 
    UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:vc]; 
    [self presentViewController:navCtrl animated:YES completion:nil]; 

}

注意,因为它已被取代presentViewController:animated:completion:presentModalViewController:animated:已弃用。