Swift: How to know which Storyboard is active - ios

I am trying to run a function only when a certain storyboard is active.
Example:
if view == secondStoryboard { //Don't know what to do here
functionOnSecondStoryboard("Test")
}
This may be a very simple fix, thanks for taking a look.

What do you mean by active storyboard? If I understand what you're trying to achieve, you can get the storyboard from which a view controller was instantiated using
let storyboard = self.storyboard
//now you can compare it to other storyboards
if storyboard == storyBoardNumberOne {
}
You can get its name with key-value coding and compare the string names
let name = storyboard?.valueForKey("name")
But be careful using the KVC to get the name - this isn't a documented feature and I cannot guarantee if an app using it would be accepted in the AppStore.

Related

Navigate to UIViewController in Swift 2.3

I am trying to navigate to a UIViewController using Swift 2.3. To be more precise, I am trying to reload the UIViewController that is currently active. I do not know which view the user currently has active, so this must be defined dynamically.
I have tried several approaches, but they all result in either compile or runtime errors.
Is something like this possible?
let activeViewIdentifier = ??? // Get currently active view identifier as a string
self.performSegueWithIdentifier(activeViewIdentifier, sender:self)
You can get like this :
Objective-C :
self.navigationController.topViewController.restorationIdentifier
Swift :
self.navigationController?.topViewController?.restorationIdentifier
I think you have some issues with your architecture; it's not the best approach to reload just everything on some View Controller you can chose;
Much better way of thinking is to determine, what exactly you want to reload and add methods to reload only thus things
Anyway, if my answer hasn't assure you, consider replacing existing view controller with new and presenting it with some animation, or without it; so your general algorithm may look like this:
Get new VC from storyboard, or creating new instance, if you don't prefer to use it
Push it over your existing controller
Reload stack of navigation controller, in which you are now
you can try this
let activeViewIdentifier = self.navigationController?.childViewControllers[(self.navigationController?.childViewControllers.count)!-1]
You can use the restorationIdentifier, it's right above the Storyboard identifier and it's a UIViewController property.
let activityIdentifierStr = activeViewIdentifier?.restorationIdentifier
self.performSegueWithIdentifier(activityIdentifierStr!, sender:self)

why does instantiateViewControllerWithIdentifier need to use "as!"

let someVC = self.storyboard!.instantiateViewControllerWithIdentifier("something") as! SomethingViewController
why do we need to use "as! DataEntryViewController"
it works when I take it out using xcode 7.3.1
Define "works".
Yes, you can instantiate a view controller without caring which subclass of UIViewController it is. And the object you get back from that method will be a view controller, so it's safe to do things with it that can be done to all view controllers: present it modally, push it onto a navigation controller, add it to a tab controller, etc.
However, if you're going to do something with it that's specific to your view controller class -- like if SomethingViewController defines a property that lets you choose which Something it displays, and you want to assign to that property -- you need to do two things:
Test at runtime that the object you got back from instantiateViewControllerWithIdentifier is the class you expect it to be. (Because depending on the identifier you pass it could be some other class, or nothing at all if that identifier isn't in the storyboard.)
Let the compiler know that the variable you've assigned that object to is typed for that class, so that the compiler will let you access properties and call methods of that class.
Assignment with an as? or as! cast does both of those things in one step. (Whether to use as? or as! just depends on how much you trust yourself to make sure your storyboard identifiers are what they claim to be, and how you want to handle failure of such assumptions.)
In fact, even if you're not using properties or methods specific to that view controller class, that as! cast adds a runtime check that your view controller class is what you expect it to be. So the fact that nothing breaks when you take the cast out isn't a sign that the cast is superfluous — it's a sign that whatever breakage the cast was checking for is not currently happening.
That is, the line you quoted would lead to a crash if somebody changed your storyboard so that the identifier "something" was on a SomethingElseViewController. If you took that cast out, you wouldn't crash there. (And you'd probably run into trouble later.)
However, if what you really want to do is assert the validity of your storyboard and program, it might be better to be clear about that:
let someVC = self.storyboard!.instantiateViewControllerWithIdentifier("something")
assert(someVC is SomethingViewController)
// then do something non-SomethingViewController-specific with it

How do I get the Storyboard name?

Im creating a new design to my app so I have created a new story board. I need to support the older design for now, but I am designing the upcoming one on the fly. I am reusing some classes and ViewControllers. Is there a way to check the storyboard name in classes? For Example, if 'main' storyboard have the normal left bar button item, however if 'newDesign' do 'x'?
I cant find a way to get the 'UIStoryboard' name?
For example in pseudocode:
if(self.storyboard.name isEqualToString: #"Main"){
}else{
}
How about this?
UIStoryboard * storyboard1 = self.storyboard;
NSString *name=[storyboard1 valueForKey:#"name"];
NSLog(#"%#",name);
Another approach is to check the restoration ID of the view controllers. How about that?

Avoid hard coding storyboard IDs in 2 places

When I want to set an ID for a View Controller I go into the storyboard file, select the ViewController, then on the Identity inspector I type in the Storyboard ID as something like "theVCID":
When I want to use that View Controller in code I do something like:
UIViewController *myVC = [myStoryboard instantiateViewControllerWithIdentifier:#"theVCID"];
Is there a way I can just hardcode this ID string in one location instead of hard coding it inside storyboard and when I use it in code?
If I could get a variable in the storyboard XML, I could just get the hardcoded string variable inside the XML source, but I don't know how/where to set up a variable so that the storyboard XML can access it.
Unfortunately you are forced to duplicate the declaration.
I wouldn't recommend parsing the XML as you will be basing that on the assumption of an implementation detail. Technically, storyboards could change their data type to anything in the future and this would break your implementation.
The safest way to do this is to keep your storyboard identifiers in a single class (say StoryBoardCoordinationController) and simply reference the storyboard objects through this single interface. Hopefully you should never be in the situation where these values are changing often, and if they are you should definitely seek to do something about it:)

Checking what storyboard (or view) the user is currently in

I am building an app with multiple table views inside it that acess PDF's which share filenames. For example I have a Storyboard A with "PDF1" and storyboard B with "PDF1" also. Changing the names of the PDF's dosent really make sense based on what I have done and the way the app works. Basically i am looking for a way to check what view the user is in? (or maybe it is called a view im not sure). What I am looking for is something like this.
if (storyboardid == "StoryBoardA")
{
//load PDF from folderA
}
if (storyboardid == "StoryBaordB")
{
//load PDF from folderB
}
I couldnt find any attributes that do something like this so any ideas are appreciated. Thank you!
Yeah, I don't see any obvious way to identify the storyboard, so until you find something better, here's a round about way of doing it. First drag out a new View Controller onto each storyboard. Under the identity inspector set the "Storyboard ID" to "OrphanVC". Under the attributes inspector set the "Title" to "StoryboardA" or "StoryboardB". Once you have an orphaned view controller set up in each storyboard, you can check which storyboard you are in with code like this
UIViewController *orphan = [self.storyboard instantiateViewControllerWithIdentifier:#"OrphanVC"];
if ( [orphan.title isEqualToString:#"StoryboardA"] )
NSLog( #"This is storyboard A" );
else
NSLog( #"Must be storyboard B" );
Note that self.storyboard is a property in UIViewController, so this only works from within one of your view controller source files.

Resources