I am setting up a basic iOS app and saw the Storyboard ID section of the Identity Inspector (as well as the Use Storyboard ID checkbox) and was curious as to whether this is actually something I need to create an app. If so what do I need to set this field to given the use below:
More specifically I am setting up 2 simple views and 2 corresponding viewcontrollers in my app. I was just trying to create a simple button to go from the first view to the second view and was using this method for the button which would ultimately transition from the first view to the second view once it was pressed:
let vc = self.storyboard?.instantiateInitialViewController(withIdentifier: "SecondViewControllerID") as! TransitionViewController
The view controller for the second view I want to be the destination of the button is named TransitionViewController. I borrowed this code from some samples and "SecondViewControllerID" is the name of the storyboard from the sample I'm borrowing from. In my Xcode, the Storyboard ID field is blank at the moment.
You need to set the storyboard id to SecondViewControllerID or ctrl-drag from the button to the destination vc through segue
Explanation :
Storyboard identifier is a unique key set for a vc inside storyboard to identify it when you load with instantiateInitialViewController
Regarding segue : CTrl-drag from the button to destination and from the popup select show
and don't write any code inside button action
The storyboard id ia mainly used when you have more than one StoryBorad.
if you have 2 storyborads you can initialize the vc by using
var vc = UIStoryboard(name: "main", bundle:nil).instantiateInitialViewController(withIdentifier: "SecondViewControllerID") as! TransitionViewController
and give "SecondViewControllerID" in Storyboard ID section of the TransitionViewController Identity Inspector.
Related
I design a soft Pin.
You can put 4 numbers twice and it will be your code. When you finished code assignment. The view will present segue (kind: Present Modally) to show another viewController.
Due to convenient, I would like to reset the View controller.
The Best way I found on the stack overflow is to reset rootViewController in the Appdelegate
Solution Here
When I follow the steps, I got an error below :
The Console shows :
Maybe the problem is the design in my storyboard
The upper one is original viewController
Can anyone figure out what's wrong with my code?
Thanks a lot!!!
The problem is from your Storyboard : you don't have a view controller with a StoryBoard ID EnterPinViewController.
Go to your Storyboard, select the controller and on side panel Utilities check the StoryBoard ID value.
Or change instantiateViewControllerWithIdentifier parameter with the one matching the Storyboard Identifier of your view controller.
Click on your controller in storyboard.
In right side panel go to identity Inspector (3rd button)
Set Storyboard ID to EnterPinViewController
You are all set to go further now!!
For the following steps:
drag two view controllers using storyboard, add a button on the first VC
ctrl drag the button on the first VC to the second VC and choose present segue
embed first VC with navigation controller
My questions are:
What happens under the hood when program starts? In particular, how and where will be the first and second VC created?
If not same, what's the difference compared with code below?
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
Swift code will be preferred but Objective-C is also fine. Thanks in advance.
What you are doing using:
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
is that you are doing what the storyboard is supposed to do. You are forcefully preparing for the segue. When you simply connect the two views using the segue on the storyboard, you are telling the compiler that the connection exists. When you run the code the second viewcontroller is instantiated the instant you click on the button and performs a prepareForSegue.
All in all, its all about manually overriding the delegate methods that are called when the storyboard works.
I'm trying to use a Storyboard ID to tell what View I'm segue to, and based on that, pass on certain information. But no matter what I do, the segue.identifier is always nil. Heres a screenshot of my settings for the view I'm segueing to:
Select the segue on the storyboard, and this is the segue identifier.
How to use Storyboard reference to connect 2 storyboards? Is it possible to connect storyboard with xib file? I want to use it via Navigation Controller.
if you want to reference two storyboards in Xcode 7, in your initial storyboard select the view controllers you want to use in the second storyboard, in the task bar select "Editor" and then "Refactor to Storyboard", then you have to name the new storyboard and thats all, i donĀ“t know if you can reference to an .xib file, notice that you can use the storyboard reference to call a view even in the same storyboard or even a view that is not connected to any other view, and in the reference you can specify which view you want to load (you do this in the identity inspector), or leave that space empty and the view loaded will be the initial view.
Better explained step by step:
If you want to create the new storyboard at the same time
select the views you want to move to the new storyboard
open the Editor's menu and select Refactor to Storyboard
Xcode will ask you the name you want to give to the new storyboard
and thats all
If you have already created the storyboard
Open the storyboard where you want to call the second storyboard
drag the Storyboard Reference that is located in the objects library
select the reference you added and open the identity inspector
in the identity inspector write the name of the storyboard that you want
to call (required) and the storyboardID of the view you want to load
(optional)
thats how you use storyboard references in Xcode 7, hope that is useful to you
You can load another storyboard simply like this your main storyboard..just replace the name of storyboard and viewcontroller
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"NewStoryboard" bundle: nil];
SomeViewController *someViewController = [storyboard instantiateViewControllerWithIdentifier:#"SomeViewController"];
[self presentViewController:someViewController animated:YES completion:nil];
or Swift
let storyboard = UIStoryboard(name: "NewStoryboard", bundle: nil)
let someViewController = storyboard.instantiateViewControllerWithIdentifier("SomeViewController") as! UIViewController
self.presentViewController(someViewController, animated: true, completion: nil)
Hi Friend this is really easy,
Just create a new Storyboard with file>newfile and get the new Storyboard from User Interface don't forget to name it properly ;)
Drag a Storyboard reference to your main story board and now connect it with button or navigation bar button or tab bar whatever (as per your requirement)and thats it...
Select Storyboard reference and go to Attribute Inspector you can see the dropdown of story board from that select your new Storyboard.
Create any new view controller in the empty Storyboard and make it initial view controller.
Run the code
This is how you do it my friend hope it helps :)
Tell me if you are not able to solve will update my answers with screen shots.
I used to have all my view controllers in the same storyboard, I decided that makes sense to split up the storyboards so I created a new storyboard file New File -> User Interface -> StoryBoard, cut all the controllers related with the user management (Login, register, password recover ...) and pasted them in the new file
Now when I call storyboard.instantiateViewControllerWithIdentifier("LoginViewController") it crashes with the following error:
'Storyboard (<UIStoryboard: 0x...>) doesn't contain a view
controller with identifier 'LoginViewController''
How can I solve that?
I think your problem is here, navigate to the Main.storyBoard after that click on your viewController which you want to initiate after that give it to the identifier here:
May be this will help you.
You have to create the new storyboard instance, and get the LoginViewController StoryboardId
//Here, create an instance of the second storyboard excluding the extension(.storyboard),
var storyBoard = UIStoryboard(name: "SecondStoryBoard", bundle: nil)
//Here instantiate view controller with the storyboard instance,
//Before that create a storyboardId for the corresponding view controller.
var loginVC = storyBoard.instantiateViewControllerWithIdentifier("loginViewController") as LoginViewController
//Here, the storyboard identifier is "loginViewController" which is created in the respective view controller's "Identity" inspector
Hope this helps, Happy Coding :)