How to Give an "Identifier" to a View Controller Within My Storyboard

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 check View controller is available with specified identifier or not

Finally, I got a solution

extension UIStoryboard {
func instantiateVC(withIdentifier identifier: String) -> UIViewController? {
// "identifierToNibNameMap" – dont change it. It is a key for searching IDs
if let identifiersList = self.value(forKey: "identifierToNibNameMap") as? [String: Any] {
if identifiersList[identifier] != nil {
return self.instantiateViewController(withIdentifier: identifier)
}
}
return nil
}
}

And I have used this method like below

let identifier = Constants.menuSections[indexPath.section-1][indexPath.row-1]

if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateVC(withIdentifier: identifier) {
let navi = BaseNaviViewController(rootViewController:viewController)
navi.navigationBar.tintColor = .white
navi.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
sideMenuController?.embed(centerViewController:navi, cacheIdentifier:identifier)
}
else {

ServerService.ShowAlertMessage(ErrorMessage: "No controller Available", title: "Oops . . . !", view: self)
}

Storyboarddoesn't contain a view controller with identifier

Make sure following field of the PageViewController is filled with identifier "PageViewController":

Sample Image

What is a StoryBoard ID and how can I use this?

The storyboard ID is a String field that you can use to create a new ViewController based on that storyboard ViewController. An example use would be from any ViewController:

//Maybe make a button that when clicked calls this method

- (IBAction)buttonPressed:(id)sender
{
MyCustomViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

[self presentViewController:vc animated:YES completion:nil];
}

This will create a MyCustomViewController based on the storyboard ViewController you named "MyViewController" and present it above your current View Controller

And if you are in your app delegate you could use

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle: nil];

Edit: Swift

@IBAction func buttonPressed(sender: AnyObject) {
let vc = storyboard?.instantiateViewControllerWithIdentifier("MyViewController") as MyCustomViewController
presentViewController(vc, animated: true, completion: nil)
}

Edit for Swift >= 3:

@IBAction func buttonPressed(sender: Any) {
let vc = storyboard?.instantiateViewController(withIdentifier: "MyViewController") as! ViewController
present(vc, animated: true, completion: nil)
}

and

let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)

Get StoryBoard from ViewController identifier

I had similar problem with my app. Multiple view controllers and multiple storyboards.
I just use the name of storyboard that contains my view controller and create new instance of it. And than I am instantiating a new view controller from that storyboard.

UIStoryboard * st =  [UIStoryboard storyboardWithName:@"storyboardWithViewControllerName" bundle:nil];
YourViewController * vc = [st instantiateViewControllerWithIdentifier:@"YourViewControllerId"];

Hope it helps



Related Topics



Leave a reply



Submit