Long Smith
Long Smith

Reputation: 1401

How to connect storyboard to code

I created empty IOS project and want to create UI. I searched for information about connecting storyboard to code but there is nothing about it, just about xib files. So is it possible to use storyboard instead xib in empty project? And how?

Upvotes: 3

Views: 6517

Answers (4)

Yohan
Yohan

Reputation: 1108

First create an empty project with some class name enter image description here

then create an storyboard from file->userInterface->storyboard

enter image description here

then give a name for the storyboard after that press storyboard in the left menu and place a viewcontroller and hold viewcontroller give class name as already created Uiviewcontrollerclass enter image description here

enter image description here

finally press project choose main interface as ViewController.storyboard

if you wanna code suppose segue use [self performSegueWithIdentifier: @"nameofcontroller" sender: self];

if don want segue go like this

[self performSegueWithIdentifier: @"TheSequeName" sender: self]; The @"TheSequeName" is defined when you ctrl drag a button to open a new view controller on the storyboard

If you don't use segue, there are many variants to open a new view controller. The basic one is using a separate NIB (without storyboard)

SecondViewController *view = [[SecondViewController allow] initWithNibName:@"NibName" bundle:nil];

If you declare your view controller on Storyboard, you can use

viewController *view = [self.storyboard instantiateViewControllerWithIdentifier:@"viewStoryboardId"]; 
Then you show your viewController using navigation controller

[self.navigationController pushViewController:view animated:YES];

Hope it works

Upvotes: 2

ipraba
ipraba

Reputation: 16543

You can get the storyboard object like this assuming you have a Storyboard file already in the Project

    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]];
    YourViewController *yourVC = [storyBoard instantiateViewControllerWithIdentifier:@"identifier"];
    //Do whatever with the viewcontroller object
    [self.navigationController pushViewController:yourVC animated:YES ];

Note:

  1. Check the Storyboard name
  2. Check your viewcontroller identifier in the Storyboard

enter image description here

Upvotes: 5

Shahab Qureshi
Shahab Qureshi

Reputation: 952

New File - > Objective-C Class (of type View Controller) -> without Xib file

now on on storyboard, go to that particular xib file

in property window -> Show Identity Inspector -> change class to above added ViewController.

That's it, now you can control your storyboard xib files from code.

Upvotes: 0

Sam Fischer
Sam Fischer

Reputation: 1472

From the 'File' menu, create a new Storyboard.

Call it Main.storyboard.

In your project settings, set it as the main interface file under the Deployment Info.

enter image description here

Upvotes: 0

Related Questions