Instead of control-dragging every cell to the other view controller, is there a way to make them all segue to the view controller more easily? I would also need to add a Segue Identifier to the individual segues as well.
That is exactly why you have a tableview delegate method.
– tableView:didDeselectRowAtIndexPath:
whatever cell in the table this method will get invoked and there you can call for whichever transition like push,model etc programmatically in code.
This sounds like something you should do programmatically rather than in interface builder. You'll want to set the selection action for the cells to trigger the Segue.
You can create a manual segue from one view controller to another. Just select view controller from the bottom bar (where you see first responder and all) and ctrl drag to other view controller. Select the segue and give it a unique identifier (TransitionSegue in this example) from attributes inspector. From code call the segue whenever you want a transition.
[self performSegueWithIdentifier: #"TransitionSegue" sender: self];
Hope this helps.
Related
I want to create two relationships(as segue) UITabBarViewController to SecondViewController via navigation controller. But these options depend on the selection on the FirstViewController. How can I do that?
Your question is a little bit weird, but I'll try to give you an answer based on what I understood.
If you want to create a UIStoryboardSegue programmatically, you cant! Segue can only be created inside storyboard.
If you want to transition from a viewController to another programmatically, you can use two method: pushViewController (of UINavigationController) or presentViewController (of UIViewController, that present a controller modally).
EDIT:
if you have already set the segues in storyboard, you can trigger them using performSegueWithIdentifier
this is odd, but I'll try to explain the best I can.
I have a navigation controller which has a view. The view asks a simple question. That view then has two segues available -- one on the view for a correct answer, which is a "show" on the navigation controller and the other for an incorrect answer which is a "popover".
The segues are tied to the view and the answer box for the show and popover respectively.
I'm testing the answer on button press and using performSegueWithIdentifier to then show the appropriate window.
if (answerField.text == "2") {
println("Correct")
performSegueWithIdentifier("Correct", sender: sender)
} else {
println("Incorrect")
performSegueWithIdentifier("Error", sender: sender)
}
The problem I'm having is that if I get the answer correct, it moves fine to the next view, but it shows the "incorrect" popover view after segueing to the correct one.
I know that sounds complicated, but it's a super simple app at present. I suspect I'm just doing it wrong. Of note is that I also get an error "Presenting view controllers on detached view controllers is discouraged" which may be related.
Many thanks for any assistance anyone can provide.
D.
It sounds like you have one or both of the segues hooked up directly to the button in your storyboard, which will trigger the segue automatically on the button press even when you don't call performSegueWithIdentifier(_:) in code. You are then setting up an IBAction method which is also called on the button press, where you are then programmatically performing the segue again.
You can check this by going to your storyboard and selecting the button. Go to the connections inspector on the right, and I expect you'll see a Triggered Segues section, with an "action" triggering your "show" segue. You can remove this by clicking the cross next to the connection.
For segues that should be initiated programmatically, you should create a segue by dragging from the view controller object (rather from a control / actionable element) to the next view controller. This will create a generic segue that is only triggered by performSegueWithIdentifier(_:) in code.
As you have noticed, since your segue is for a popover presentation it will complain unless it is explicitly anchored to a particular view in your storyboard. You can still drag the segue from the view controller object to the popover view controller, but you must manually hook up the anchor: select the segue in the storyboard, choose the attributes inspector on the right, and drag from the circle in the "Anchor" field to the text field you want to anchor to.
I've created a UIViewController, and insert a container view in it, which embed a UITableViewController. As the image described above.
When user click the Table View Cell, I'd like to add a UIBarButton on the Navigation bar.
But, how can I manage this? I can rise the DatePicker when click on Table View Cell, but when I call self.presentingViewController in table view controller implementation file, it returns (null), same as when I call self.parentViewController
You're probably trying to access the parent controller too early. If you log self.parentViewController in viewDidLoad, it will return null, but it should return the correct controller from viewDidAppear, and certainly from the didSelectRowAtIndexPath method. Using parentViewController in this context is correct, not presentingViewController.
I'd suggest to implement the UITableViewController Delegates and Datasource Methods in the ViewController itself.
That way you don't have to worry about accessing the ViewController containing the UITableView.
I'm currently facing something I find rather strange.
In my storyboard, there is a TableViewController filled with static cells.
I would like to perform a simple segue (a push to another view controller) when I select one of the rows.
So I "ControlDrag" from the concerned table cell to the sibling controller, but when tap one the cell, nothing happens. Next to that, I did try assigning the segue triggering to the accessory view of the cell (a disclosure button). And in that case, the segue is effectively triggered.
So here is my question : Should I use the "programmatic way" to handle the tap on the cell (tableview delegate methods and manual segue performing), or is there something I'm missing in the interface builder? And btw how could we explain the behavior difference with the accessory button view ?
Here is two screenshot
Cell selection that should trigger segue
And, the accessory action which performs the segue
I finally found what the problem was. I had a gesture recognizer attached to the view controller, and it was catching the tap event on the cell.
I have the following storyboard:
In the tab bar controller I have a add button with a segue to a view controller. I need to use the method "prepareForSegue:" because I need to transfer an object but in the Repositories Table View doesn't fire the method.
How can I detect the method "prepareForSegue:" in the Repositories Table View? Is it possible?
I suppose for "detect the method prepareForSegue" you mean "identify the right segue in the prepareForSegue method".
You do this by using [segue identifier] and the string comparison method isEqualToString: to compare it to what you set the Identifier to be in storyboard.
Also, make sure that the button, table view cell, button or other element is attached to the right segue. You can of course do that in storyboard as well. If you need to do it in code e.g. because the button is created programmatically, you will have to call the segue yourself. In the button handler you just use
[self performSegueWithIdentifier:"SegueIdentifier" sender:self];