How to programmatically access the main interface - objective-c

I've been using the following code untill now to get the storyboard:
NSString * storyboardName = #"myStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
However, I now have a project with multiple targets, each with their own storyboard. This code no longer works, because the name of these storyboards is different for all targets. Is there a way to get the storyboard that is set as "main interface" in my target, without knowing the name of the storyboard at runtime?

In your UIViewControllers
Use
Storyboard *storyboard =self.storyboard;
instead of
Storyboard *storyboard =[UIStoryboard storyboardWithName:storyboardName bundle:nil];

Related

possible to add storyboard to non storyboard project?

I am new to Objective C, but with experience of C#. I have a project named "chat secure" (on github), but I don't know how the project works without storyboard. I would like to add storyboard to the project. Is it possible to use Storyboard along with other project?
Try this code
ShopObject *obj = arrDeals[indexPath.section];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"SwipeDealViewSB" bundle: nil];
StoryBoardViewController *swipeDealViewController = [storyboard instantiateViewControllerWithIdentifier:#"StoryBoardViewController"];
[self.navigationController pushViewController:swipeDealViewController animated:YES];
But remember to name your storyboard identify and custom class is"StoryBoardViewController" demonstrate image to identify storyboard

Manually open a NSWindow from a Storyboard

For an OSX app I have the UI in a storyboard, with the initial controller being a NSWindowController with a NSWindow attach to it. It works just fine when the window is opened automatically, but I can't get it to work manually, which is needed as the window should only open based on a human interaction.
NSStoryboard *storyboard = [NSStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
NSWindowController *vc = [storyboard instantiateInitialController];
NSWindowController *windowController = [[NSWindowController alloc] initWithWindow:vc.window];
[windowController showWindow:nil];
[windowController.window makeKeyAndOrderFront:self];
[windowController.window makeMainWindow];
[NSApp activateIgnoringOtherApps:YES];
In a different project it does seem to work when the view controller is a separate XIB file, but in this case, it doesn't. I do see an empty, rectangular window appear briefly, so something is happening. Any ideas on how to get this to work?
Here your windowController object is local to the selecor, it is getting deallocated when the selector which contains the code is returned.
Make windowController, a property of the object which contains this code.

Accessing the same view controller in a Storyboard by a subclass

Hullo,
I would like to create a new target for a project without overcrowding the main view controller with #ifdef's. So I am thinking of splitting its functions with a weaker superclass and have the new target use the top one. How do I configure the new target to attach the top view controller to the view controller in the storyboard instead of the bottom one as in the old target?
You would have to invoke and set the view controller manually like so:
UIStoryboard *storyboard = [UIApplication sharedApplication].keyWindow.rootViewController.storyboard;
UIViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:#"yourViewControllerIdentifier"];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:myViewController animated:YES completion:nil];

Unable to switch between UIViewControllers using storyboard

I am new to objective C so please forgive me if this is a stupid question (and I have searched!)
I am using storyboards in xcode (4.4.1) and I have 2 UIViewControllers
I am trying to programmatically switch between them (not sure if that's the right terminology?)
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"BookingView"];
[self presentModalViewController:vc animated:YES];
The above code works perfectly later on in my project, but I when i try call if in an if() in my viewdidload method in the first UIViewController it doesn't work. Help please!
You shouldn't perform such task in the viewDidLoad: method.
You're trying to present a view controller modally on top of another that's only been allocated in memory and loaded, but it's not visible yet. That means your presentation won't work. You should move your code to viewDidAppear:
I recommend you to read this guide to learn the basics about view controller delegation: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html%23//apple_ref/doc/uid/TP40007457
Why not self.storyboard instead of [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];?
Assuming that all your view controllers are from the same storyboard :)

Show view from storyboard chain

I have a chain in storyboard and I want to start (for example) second view when I have first launching app. I have some code, which only work in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
method, if I put it in
- (void)viewDidLoad
method it's not work,
the code which show my other view is:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"someId"];
[vc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
How can I show view, which have place in chain in storyboard when app have first launching?
Try your code in
-(void)viewDidAppear:(BOOL)animated.
For further information on the iOS viewcontroller lifecycle head over to UIViewController class reference
If you are using a navigation controller, you can simply push the second view onto the navigation stack. If you are not using a navigation controller, then you could force a segue to the next view from the loading.