How to call a View Controller programmatically?

前端 未结 8 2247
孤街浪徒
孤街浪徒 2020-12-02 07:04

I have looked at all the tutorials I can find on this one, and I still don\'t have the answer. I need to call another view from the code. I am using UIStoryboards

8条回答
  •  -上瘾入骨i
    2020-12-02 07:41

    You can call ViewController this way, If you want with NavigationController

    enter image description here

    1.In current Screen : Load new screen

    VerifyExpViewController *addProjectViewController = [[VerifyExpViewController alloc] init];
    [self.navigationController pushViewController:addProjectViewController animated:YES];
    

    2.1 In Loaded View : add below in .h file

    @interface VerifyExpViewController : UIViewController 
    

    2.2 In Loaded View : add below in .m file

      @implementation VerifyExpViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.navigationController.delegate = self;
        [self setNavigationBar];
    }
    -(void)setNavigationBar
    {
        self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
        self.navigationController.navigationBar.translucent = YES;
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"B_topbar.png"] forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
        self.navigationItem.hidesBackButton = YES;
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Btn_topback.png"] style:UIBarButtonItemStylePlain target:self action:@selector(onBackButtonTap:)];
        self.navigationItem.leftBarButtonItem.tintColor = [UIColor lightGrayColor];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Save.png"] style:UIBarButtonItemStylePlain target:self action:@selector(onSaveButtonTap:)];
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor lightGrayColor];
    }
    
    -(void)onBackButtonTap:(id)sender
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    -(IBAction)onSaveButtonTap:(id)sender
    {
        //todo for save button
    }
    
    @end
    

    Hope this will be useful for someone there :)

提交回复
热议问题