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?
Related
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.
I am trying to manually push a view controller within my iOS 8 app. I have designed it in the Main.storyboard and i have already attached on it an specific identifier.
The code i am using is:
CustomViewController *vc =
[self.storyboard instantiateViewControllerWithIdentifier:#"CustomViewController"];
vc.customField1 = self.customField1;
vc.customField2 = self.customField2;
[self.navigationController pushViewController:vc animated:YES];
but that causes the app's freeze. It does not spit out any logs or something, so I cannot understand what might be wrong.
Can you help me a bit here?
Thank in advance
Do not do these two lines:
vc.customField1 = self.customField1;
vc.customField2 = self.customField2;
The problem here is that you're assigning one text field to be another text field (actually, you're making a text field reference refer to a completely different text field). Instead, copy the contents (e.g. the text) of the fields from your parent view controller to fields that already live in your new CustomViewController:
vc.customField1.text = self.customField1.text;
vc.customField2.text = self.customField2.text;
I'm thinking what is happening here is that the app is hanging when the new CustomViewController appears because it's trying to access fields in the now hidden / pushed-away parent view controller.
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.
I need to work out how to identify what storyboard is active at any given time. I have a specialised nativation in which I need to identify the storyboard (UIView) then change things programmatically depending on what the user presses.
All storyboards have Identifiers.
in the viewDidLoad of the root view I have the following.
- (void)viewDidLoad
self.topViewController =
[storyboard instantiateViewControllerWithIdentifier:#"View1"];
{
What I would like to do is identify which storyboard the user is on and depending on the press do the following sudo-code
- (void)viewDidLoad
if (storyboard.name != RootView)
self.topViewController =
[storyboard instantiateViewControllerWithIdentifier:#"View1"];
{
else if (storyboard.name = View2){
self.topViewController =
[storyboard instantiateViewControllerWithIdentifier:#"View2"];
}
etc....
I have step through the code and seen the StoryboardID however it's which I'm pretty sure your not meant to use....
Thanks in advance
Jeremy
UPDATE: Explanation to Navigation
I'm trying to implement the ECSlideViewController, but It's doing my head in. Effectively adding in the slide to the right function to reveal more options. SO, this thinking was going to be easy turned out icky. I have the master UIViewController<title:HomeView> I then have 4 buttons on the screen which segueway to other UIViewControllers<View1>, UIViewController<View2> etc.
In order to produce the effect on View1,View2,View3,View4 I need to bring the class (ECSlideViewController as per the example) into the UIViewController<HomeView>. However If I change the below code to represent this...
self.topViewController =
[storyboard instantiateViewControllerWithIdentifier:#"HomeView"];
It crashes because it calls itself. Not good, circular coding is a no no.
but if I set it to what was originally there
self.topViewController =
[storyboard instantiateViewControllerWithIdentifier:#"FirstTop"];
( btw firstTop is the title of the view used with the example)
It works but then disregards the UIViewController<HomeView>
This is why I asked if there was a way to identify the self.title of the UIViewController(said storyboard...my bad) as I was going to put conditional logic around it in order to not put the code in if it's on the UIViewController<HomeView>.
It really is hard to explain unless you download the ECSlideViewController and start playing with it. But effectively I just want to test the self.title.....I think...
The other idea was to bring the logic into the UIViewControllers of the four and get it to work there...but It freaks out passing nil as it's expecting an identifier...
Hope this makes sense....
J.
Okay Guys,
Totally ditched ECSlideViewController. I found a few articles explaining that it had issues when you had multiple UiViewControllers not passing data correctly. I ended up using Andrews suggestion. http://www.youtube.com/feed/UCJA_puohXgnze8gPaerTeig It worked for easier for me.
Although I take note of what the design guidelines Apple have an this is usually a no no, but I'm hoping that they won't mind.
Thanks everyone again!
J.
I'm not sure if this helps, but it sounds like you will have to compare instances to get the results you are looking for. I haven't tried this, but I would create properties of each storyboard in your app delegate, then reference the app delegate in your view controller to compare. This might not be the best coding practices, but I'll leave that into your hands.
Something like (untested code):
- (void)testStoryBoard //After awake from nib or viewDidLoad
{
NXAppDelegate *appDelegate = (NXAppDelegate *)[[UIApplication sharedApplication] delegate];
if ([self.storyBoard isEqual:appDelegate.view1StoryBoard])
NSLog(#"View1 Storyboard");
else
NSLog(#"View 2 Storyboard");
}
Hello I am making an app that requires a random page generator so I wanted to know if there was a way to add a random page generator to storyboard in xcode
Nothing out of the box that I know of (and given that every application has different concepts of "pages" I'm not sure that there could be a generic approach. (I don't use storyboards, though, so I'm somewhat outside my comfort zone.)
It's easy enough to generate random numbers, which you could use as inputs to some method that will, say, pick out an indexed view controller and push it onto the navigation stack. But this is hypothetical and entirely dependent on your application architecture.
Care to elaborate?
if your intention is to generate random page (meaning view controllers) from a single command (e.g. Button) you can try this approach:
In the storyboard, simply link the homeviewcontroller to the numbers of random view controllers you intend to create, by pressing control-drag and select push as the segue.
Identify the numbers of segues independently, e.g segue1, segue2, ... segue100000 etc
Lastly, just key in the following coding according to your headers and implementation names;
For the header of the homeviewcontroller.h
-(IBAction)RandomButton:(id)sender
For the implementation of the homeviewcontroller.m
-(IBAction)RandomButton:(id)sender
{
NSArray *segues = #[#"segue1", #"segue2", ... ,#"segue10000"];
NSString *segueID = segues[arc4random_uniform(segues.count)];
[self performSegueWithIdentifier: segueID sender: sender];
}
Now try to run it. It should work fine! Cheers.