Write an iOS/iPadOS app code to transition from one screen to another.

There are instances when you want to transition or navigate from one UI screen to another by pressing a button.

Yes, you can link the button visually by control-clicking on it and then linking it to the required UI Screen on the storyboard.

But I will show you how you can do it programmatically with code defined & working differently for iPad & iPhone Storyboards.

I have used iOS Conditional coding syntax to differentiate iPhone & iPad Storyboards

Note: You can read about iOS Conditional Coding Here, How to detect user is using iPad or iPhone for iOS App at run time conditional coding purposes

Step 01:
Use the following code for your choice of UI Button IBAction Sender button

- (IBAction)exitButton:(id)sender {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        UIViewController *obj = [[UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];
        [self.navigationController pushViewController:obj animated:YES];
    } else {
        UIViewController *obj = [[UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];
        [self.navigationController pushViewController:obj animated:YES];
    }
}

Step 02:
Now navigate to the Storyboard right-hand panel for ‘Storyboard ID Property’, which can be found in the Identity Inspector of the View Controller:

  1. Open up your storyboard in your Xcode project
  2. Make sure you have the Identity Inspector open on the right-side panel, i.e., the Third icon at the top when you have the Utilities pane open
  3. The second section in the Identity Inspector is labelled Identity. The first property listed is Storyboard ID. You have to set its value to ViewController.

Now you are good to go,

Try to run your app in a simulator or on your device to check whether it works.

Hope it helps,

Thanks & Regards
Mandar Apte

Published by Mandar Apte

Mandar is a Mumbai-based multi-disciplinary designer with UX/UI, Logo, Symbol, and Brand Identity design expertise. He currently runs his Mudrkashar Linguistic Apple iPhone, iPad, and Mac app business in the heart of Mumbai city.

Leave a comment

Leave a Reply