How to call a View Controller programmatically?

前端 未结 8 2245
孤街浪徒
孤街浪徒 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条回答
  • 2020-12-02 07:20

    You need to instantiate the view controller from the storyboard and then show it:

    ViewControllerInfo* infoController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerInfo"];
    [self.navigationController pushViewController:infoController animated:YES];
    

    This example assumes that you have a navigation controller in order to return to the previous view. You can of course also use presentViewController:animated:completion:. The main point is to have your storyboard instantiate your target view controller using the target view controller's ID.

    0 讨论(0)
  • 2020-12-02 07:23

    There's 2 ways you can do this:

    1, Create a segue to your ViewController in your Storyboard as explained in my answer here: How to perform a segue that is not related to user input in iOS 5?

    2, Give your ViewController and identifier and call it using the code in my answer here: Call storyboard scene programmatically (without needing segue)?

    0 讨论(0)
  • 2020-12-02 07:31

    Swift

    This gets a view controller from the storyboard and presents it.

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let secondViewController = storyboard.instantiateViewController(withIdentifier: "secondViewControllerId") as! SecondViewController
    self.present(secondViewController, animated: true, completion: nil)
    

    Change the storyboard name, view controller name, and view controller id as appropriate.

    0 讨论(0)
  • 2020-12-02 07:37
            UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone_iOS7" bundle:nil];
                AccountViewController * controller = [storyboard instantiateViewControllerWithIdentifier:@"accountView"];
                //            [self presentViewController:controller animated:YES completion:nil];
    
            UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
            while (topRootViewController.presentedViewController)
            {
                topRootViewController = topRootViewController.presentedViewController;
            }
    
            [topRootViewController presentViewController:controller animated:YES completion:nil];
    
    0 讨论(0)
  • 2020-12-02 07:40

    Import the view controller class which you want to show and use the following code

    KartViewController *viewKart = [[KartViewController alloc]initWithNibName:@"KartViewController" bundle:nil];
    [self presentViewController:viewKart animated:YES completion:nil];
    
    0 讨论(0)
  • 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 <UINavigationControllerDelegate>
    

    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 :)

    0 讨论(0)
提交回复
热议问题