Usually in iOS we setup buttons in our ViewControllers, and then segue to new ViewControllers by clicking and control dragging the button to the new ViewController we want to transition to.

But what if you add a custom button to your UI, and you no longer have the the ability to control drag to your new ViewController. How do you tell the button which view to pop up?

Answer: Instantiate the view from the story board, and pragmatically pop your view controller from there.

Here’s where I manually at my own button to the NavBar:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStylePlain target:self action:@selector(gotoAlarmSettings)];
    self.navigationItem.rightBarButtonItem = anotherButton;

}

And then here is how you instantiate a view from the storyboard:

- (IBAction)gotoAlarmSettings {
    AlarmViewController *alarmView = [self.storyboard instantiateViewControllerWithIdentifier:@"foobar"];
    [self.navigationController pushViewController:alarmView animated:YES];
}

Note: You set the identifier for your viewController in the story board (else they won’t connect).

 

If you don’t do it this way, and you instead just popup it up regularly like this:

    AlarmViewController *alarmView = [[AlarmViewController alloc] init];

The transition will work, but you will get a black screen for your view.

Advertisements