How to Programmatically Get the Identifier of a Basic Uiviewcontroller from an Uiviewcontroller or Storyboard in Swift/Objective-C

Is it possible to programmatically get the identifier of a basic UIViewController from an UIViewController or Storyboard in Swift/Objective-C?

In Swift:

 var str: String! = self.restorationIdentifier

programmatically access UIViewController identifier

This question solves my problem for me:

Programmatically get a Storyboard ID?

I am just using the restorationId instead, works perfectly.

Programmatically get a Storyboard ID?

You can use the restorationIdentifier, it's right above the Storyboard identifier and it's a UIViewController property.

Instantiate and Present a viewController in Swift

This answer was last revised for Swift 5.4 and iOS 14.5 SDK.


It's all a matter of new syntax and slightly revised APIs. The underlying functionality of UIKit hasn't changed. This is true for a vast majority of iOS SDK frameworks.

let storyboard = UIStoryboard(name: "myStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "myVCID")
self.present(vc, animated: true)

Make sure to set myVCID inside the storyboard, under "Storyboard ID."

How do I give an identifier to a view controller within my storyboard?

As the Image suggests! Hope it helps!

Sample Image

Then to use it you call:

[self.storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerID"];

How to load UIViewController programmatically from storyboard?

The way you're doing this just creates a new instance of your view controller. It does not create one from the prototype you've defined in Interface Builder. Instead, you should be using this, where "SomeID" is a storyboard ID that you've assigned to your view controller in Interface Builder.

if let resultController = storyboard!.instantiateViewControllerWithIdentifier("SomeID") as? ResultViewController {
presentViewController(resultController, animated: true, completion: nil)
}

You can assign a storyboard ID to your view controller in Interface Builder's identity inspector.

Sample Image

iOS how to get a reference to a view controller embedded in a storyboard container with a segue?

you must implement the prepareForSegue in main ViewController and specify an identifier in your StoryBoard.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];

}
}


Related Topics



Leave a reply



Submit