Swift Segue Issue - ios

I am interested in segueing from an xib file (when a button is tapped) which is one table view cell from a table view, to another view controller. I am unable to show segue (through main storyboard) because they aren't on the same storyboard. I was wondering if this could be done programmatically, and if so, with what kind of code.
Please let me know if you need more detail.

Your options for programmatically showing a view controller are the following functions
From within a view controller
presentViewController
dismissViewController
From within a view controller on a navigation stack
pushViewController
popToViewController
popToRootViewController
popViewController
Which function to use is determined by the context in which the view controller appears.
For presenting view controllers, if you are working with XIBs and storyboards then the usual pattern is to first instantiate a view controller from the XIB or storyboard and then present it using presentViewController.
An example is
let vc = UIViewController(nibName: "NAME_OF_THE_NIB", bundle: NSBundle.mainBundle())
self.presentViewController(vc, animated: true) {
// Add completion code here.
}
With navigation stacks you can use the convenience functions for pushing and popping view controllers.
To be clear about terminology, none of these methods are considered to be a Segue. That term only applies to moving between view controllers in a storyboard.

If your cell and your view controller are in different storyboards, you can (since iOS 9) use a segue between them with storyboard references.
See Adding a Segue Between Two Storyboards.
If your cell is in its own .xib unfortunately it is not possible to use a segue.

Segue is between view controllers. So you can't do that in xib as it is only views. You should present/push view controllers in table view's view controller. You can get its view controller like this:
tableView.viewController // and then present/push

Related

UINavigation issue in App

i have multiple storyboard in my storyboard. I have embedded the firstVC with navigationcontroller and remaining attached to the firstvc through segues. The remaining VC's are not embedded in navigationcontroller. Now when i click the button to open the next VC without navigation controller. It opens it twice. When i disconnect the segue it does not open the VC. I'm confused that why this is happening so. This is how my UI Looks,
This is how i perform the segue,
self.performSegue(withIdentifier: "GoToNextVC", sender: self)
View Controllers operate on a stack hierarchy. By embedding your first view controller in a UINavigationController and setting that as the Initial View Controller, you're making it the root view controller for the whole stack. The issue you are experiencing is occurring because you are segueing to view controllers without the root view controller's knowledge every time you call performSegue(withIdentifier:sender:). This causes the stack to be out of order and disjointed, preventing you from returning to the previous view controller(s) when calling dismiss(animated:completion:).
Instead of segues, try pushing each view controller to the stack with:
navigationController?.pushViewController(VIEW_CONTROLLER, animated: true)
To use this:
Remove any segues in Interface Builder, they are no longer needed.
Replace VIEW_CONTROLLER with the view controller you want to push.

Segue from one view controller embedded within UINavigationController to another UINavigationController

I have a UINavigationController as my root controller, but then I segue from one view controller to another UINavigationController.
But when segueing from one view controller which is embedded within a UINavigationController to another UINavigationController the push segue comes from the bottom, presuming it is segueing as a popover. I tried using a show detail segue but still not luck.
Why is this occurring and how can I segue from one to the other using a push/replace segue ?
PS: Is this happening because the UINavigationControllers conflict and overrides the segue as a popover ? The reason I am using two separate navigation controllers is because the style from the previous view overrides the style of the detail view, i posted a separate question about that Cannot change style of UINavigationBar when using scrollViewDidScroll on separate View Controller
Push segue occurs only within one navigation controller. It's how it implements 'show' type segue. Making 'show' segue from navigation controller to another navigation controller is not what Xcode drawing tool considered as 'pushing'. It interprets it as popover by default.

Adding viewcontrollers to navcontroller with storyboard

I have a basic storyboard setup where I load my NavViewController, which then points to initial view controller. I then have several additional view controllers all daisy chained together via segues in a linear fashion. When I initially launch my application i run the following in my NavControllerViewController.m
(void)viewDidLoad
{
[super viewDidLoad];
NSArray * controllerArray = [self viewControllers];
NSLog(#"view controllers: %#", controllerArray);
}
The log only shows the very first root view controller (the one directly 'connected' to the nav controller). All over view controllers are missing from the stack. I was under the impression that if a view controller was on my storyboard that it would automatically be added to the nav controller?
If this is not correct, would a good alternative be to instantiate each VC, from the calling VC? For example, if I wanted to transition from VC1 to the VC2, would I put the following code in VC1:
UIViewController *vc2 = [self.storyboard instantiateViewControllerWithIdentifier:#"vc2"];
[self pushViewController:vc2 animated:YES];
Or possibly:
[self performSegueWithIdentifier:#"vc2Segue" sender:self];
They are all able to be reached by segues, but they are not instantiated or pushed onto the stack until you segue to them.
You could use the self perform segue tactic, or the push view controller, or, alternatively, if it is in response to a single button click, just control drag from that button to the next view controller and Xcode does all the rest of the work for you.
If I understand your question correctly, you have a storyboard setup looking similar to the below screen shot.When an application loads this storyboard it will definitely have only one view controller in the navigation controller stack and that will be the root view controller.
Because other view controllers are still not pushed into the navigation controller stack.
In the Viewcontroller-1, you could see a button some Action, I have created a push segue from that button to the Viewcontroller-2.Once you tap that button, second view controller will be pushed to the navigation controller stack.
If you print the viewcontrollers count now, you should get the count as 2.
Repeat the same in the View Controller-2, now you can see the count bumps to 3. Because now we have three view controllers pushed into the navigation controller stack.
Press the back button to pop the view controllers and could see the view controllers count coming down, that's because view controllers are now being removed from navigation controller stack.
As AdamG stated, the UIViewControllers will not be pushed on to the stack until you segue to them.
To set a segue, select a UIViewController and control+drag the connection to the target UIViewController. Under the Attributes Inspector tab set the Storyboard Segue Identifier.
To segue to a UIViewController use the method performSegueWithIdentifier:. Before performSegueWithIdentifier: is called prepareForSegue:sender: will be called. This is where you can pass any values that the next UIViewController needs. To check which segue is being called use the segue.identifier property in prepareForSegue:sender:. After that, you can access the destinationViewController property.
If you need to manually instantiate a UIViewController use instantiateViewControllerWithIdentifer:. The identifier can be set under the Identity Inspector tab in the storyboard.

Cannot see controlls of presented view that I dragged on stroyboard after calling presentModalViewController

I am a newbie in iOS developing. I am trying to accomplish a sample from a book, but I use storyboard instead of xib. I added some buttons to one of the view and assigned a viewcontroller to it. I tried to call presentModalViewController to show the view in a action, but it seems nothing on the presented view. I add some code in viewdidload, and it works. But why can I see the buttons that on storyboard?
You should be using performSegueWithIdentifier:sender:, rather than instantiating and presenting the view controller yourself. Only then is it deserialized from the storyboard.
If you really want to just use the storyboard as a holding area for your transitions, deserialize it with [self.storyboard instantiateViewControllerWithIdentifier:#"whatever"] and present the result. But that's not how storyboards are intended to work; the intent is to specify both the destination and the transition in your segue. That it's presented as a modal is just a part of the segue you define.
Think of it this way: A storyboard is not a direct replacement for a XIB. A XIB defines a single view controller's contents. A storyboard not only defines many view controllers and their contents, but how the view controllers interact with each other.

UINavigationController shows black screen after pushing a view

I have a storyboard application with a navigation controller an two views controllers ('A', 'B').
In the Storyboard file:
Navigationcontroller is the initial view controller. view controller 'A' is connected to the Navigationcontroller as rootcontroller. View controller 'B' is in storyboard but not connected to any view controller.
when i programmatically try to push view controller 'B' onto the navigationcontroller from inside view controller 'A' with:
B *controllerB = [[B alloc] init];
[self.navigationController pushViewController:controllerB animated:YES];
all i get is a transition to a black screen.
i checked the navigationController property in view controller A at runtime and it´s not nil.
I do not instantiate the navigationController by myself, I let storyboard do the work (maybe that´s the problem). But I think it should be possible to "manually" push view controller to a navigation controller created by storyboard.
When I connect a segue from a button to 'B' in storyboard everything works fine.
Only programmatically it does not work, only shows a black screen inside the navigationcontroller.
Maybe someone could help me with this issue.
I haven't used storyboards yet so this answer is from a glance at the docs. It looks like you can't alloc/init a view controller from a storyboard. You need to use the instantiateViewControllerWithIdentifier: method of your UIStoryboard instance and then you can push the controller.
The accepted answer didn't work for me. I was already using instantiateViewControllerWithIdentifier to navigate to view controller in different storyboard. I am using xcode7.2 and Swift.
I am navigating from storyboard1, some view controller's button action.
Destination is to initial Navigation controller of Storyboard2.
Still I get black screen.
The problem was storyboard2's Navigation controller linked to 1st view controller was linked via show.
Solution:
Delete the link between Nav controller and 1st view controller. Now link it using root view controller. (Ctrl+Click on Navigation controller and drag it to the View controller and Select the option "root view controller")

Resources