iosuiviewcontrollerseguepushviewcontroller

Launch different ViewControllers on a button click in iOS


My project is in iOS6.I have three UIViewControllers on my iPhone storyboard. On the first UIVC i have a checkbox and a continue button. If checkbox is checked and button is clicked then I have to launch second UIViewController.If checkbox is unchecked and button is clicked launch the third UiViewController. How do i do it? I have done this so far:

- (IBAction)btnContinue:(UIButton *)sender
{
if (self.shipToDifferentAddress)//Checkbox CLICKED->YES
{
    //Launch ShippingVC
    ITMShippingAddressVC *shippingVC = [[ITMShippingAddressVC alloc]init];
    [[self navigationController]pushViewController:shippingVC animated:YES];
}
else //Checkbox unchecked ->NO
{
    //Launch OrderedItemsVC
    ITMOrderedItemsVC *orderedVC = [[ITMOrderedItemsVC alloc]init];
    [[self navigationController]pushViewController:orderedVC animated:YES];
}
}

The self.shipToDifferentAddress is a bool variable telling launch this or that. I logged both the viewDidLoads and i got the output from both of them.There is some content on both the second and third UIViewControllers like buttons, labels etc. But they appear black.No content. I have back buttons on both of them and I can traverse back to first UIVC from either one of the one. SO what am i missing? I used segues incases where I had to go to a specific UIVC. Now i have a choice either second or third. How should i do it?Please let me know if you need more information. Thanks.


Solution

  • To present your ViewControllers that are in your storyboard, follow this:

        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                                 bundle: nil];
        ITMShippingAddressVC *viewController = (ITMShippingAddressVC*)[mainStoryboard
                                                                             instantiateViewControllerWithIdentifier: @"ITMShippingAddressVC"];
    

    Set the Identifier in your storyboard.. click on your view, and in the right properties menu set the Storyboard ID enter image description here

    Than you can push like normal:

    [[self navigationController]pushViewController:viewController animated:YES];
    

    While this will work for you-- when using Storyboards, ideally you want to use Segues. It requires much less code. Here is a good tutorial on using segues to push/present view controllers and pass data between them.