How to return to another UIViewController from Table Cell - ios

I have 2 UIView for my app, one main view, and one detailed view. From the main View, I open a detailed detailed view, where I have a table, with a segue. I want to return to the main view when clicking in one of the cells of this table. As I want to return some data, I set up a segue, but yet I was not able to run the code in the segue, and then return to the main view. It works when adding a button (exit), but not when using the code
I already checked this:
Using delegates to transfer data from one TableView to another
and lots of others, but I didn't found somebody who has the same problem.
Do you have any idea how to solve this?
Thanks a lot,
Alexander

In Storyboard, you can connect an unwind segue from the cell in your detail view to the unwind method you added in your main view controller.
Don't add any code to the detail's didSelectCellAtIndexPath: as it will not run before the segue.
Instead, set any properties you want to return in prepareForSegue.
The unwind segue will be called when the cell is selected, and the main view controller will be able to access those returned properties in its unwind method.

Related

UINavigationController for infinite navigation (nested folders)

I need to navigate inside folders and files in directory (from server). The problem is that I don't know the number of folders so it's not possible to use performSegueWithIdentifier statically. How can I use navigation controller with dynamically number of view controllers in swift? I want to "push" a new view controller every time a user tap on a folder in order to list files/folders inside it and I want to do it with UINavigationController so the user have the possibility to go back with "previous" button.
Both storyboard and programmatically approaches are ok.
Thanks you
Storyboards and segues are just a crutch. Think about how you would do this without them. At each level, to go down a level, you would just instantiate a new view controller and push it onto the navigation controller stack with pushViewController:animated:.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/pushViewController:animated:
And in fact it takes only one view controller class to do this, since any instance can create and push another instance of its own class. The display of one folder is exactly like the display of any other.
So if you wanted to configure this notion in a storyboard, you would have a circular segue, that is, the view controller would have a push / show segue leading to itself.
I agree with #matt's answer, just create the controller and push it. For sake of completeness, you can do this in a Storyboard with a segue.
Here's how:
So that you can call the segue programmatically, add an additional prototype cell to your tableView. (You do this because you don't want the segue to be automatically triggered when the tableViewCell is selected. By using an additional prototype cell, the segue can be wired up, but it will never be triggered automatically since this prototype cell will never actually be instantiated.)
Control-drag from this prototype cell to the viewController icon at the top of the tableViewController. Select "Show" from the pop-up.
Find this segue in the Document Outline View and give it an identifier such as "showFolderSegue" in the Attributes Inspector.
Now, when you want to trigger the segue, call: self.performSegueWithIdentifier("showFolderSegue", sender: self)
You can use prepareForSegue to set up the new tableViewController as you normally would.
This method too works with a single tableViewController.

Using storyboarding in xCode how can I change the value of a label from a different view?

I'm writing an app that uses storyboarding and I want to update the labels in one view by clicking a button in a previous view.
_label.text = variable1;
is the line I would use to change the value of label, which is in the next view, when I click the button. Using this method I can easily change labels in the same view as the button but it does nothing when I go to the next view and see empty labels.
I've tried looking everywhere and found similar issues but couldn't find anything that worked for me so any solution would be very appreciated!
Unfortunately, it is not possible to connect IBOutlets between different scenes in storyboard.
It is difficult to suggest some precise solution because you have to provide more details about the setup which you have. Still, it is possible to outline some possible solutions:
Using prepareForSegue
If the view controller which you want to modify appears after the segue is performed you can customise its appearance in prepareForSegue function.
Using delegation
You can assign the view controller which wants to modify another view controller as its delegate. For example, if ViewController1 wants to modify ViewController2:
#interface ViewController1: UIViewController {}
#property (nonatomic,weak) ViewController2 *controllerThatIWantToModify;
with such setup you can call:
self.controllerThatIWantToModify.label.text = variable1;
You use storyboards, so there must be a segue from your first viewController (with the button) to your second (with labels in it).
If it is the case, you can set up the labels of the second view controller from the prepareForSegue method of your first view controller.
This method is called with a segue object which has a destinationViewController property which is your second view controller.
If you have several segue from this viewController, you should check if it is the right segue and then set it up.
To do that you need to set up outlets that gives you access to the labels from the viewController.
Then you can either write a setUpLabelsWith:(NSString)text1 ... method in your view controller, or directly access the outlets from the first view controller (supposing their are not private).
Yes this supposes your second view controller has a custom class.

Segue in UICollectoinView using Xcode 6

I am new to Swift and iOS development.
I am trying to create a demo app which simply shows details of an item in a new scene when clicked.
I tried using Storyboard but I learned that One cannot create segues directly from cells in a storyboard because the CollectionView is populated dynamically through the data source.
So I tried using performSegueWithIdentifier:sender but clicking an item doesn't trigger anything. I have set the Identifier for the Segue in Storyboard. I can't figure out what's wrong (As I am pretty new and don't understand the complete flow yet).
My first class is myFirstViewController.swift and second is mySecondViewController.swift
Please help (Where should I put the prepareForSegue, performSegue function?)
I am using xCode 6 on OS X Yosemite.
Your myFirstViewController class must implement the UICollectionViewDelegate protocol.
In InterfaceBuilder create a segue to your second viewController by ctrl-dragging from the viewController1 icon to the second viewController. Give that segue an identifier name in the property inspector.
In myFirstViewController.swift, implement the collectionView(_:didSelectItemAtIndexPath:), there you can call performSegueWithIdentifier with your segue's identifier name.
You can segue directly from a cell in storyboard. Simply right-click and drag from the cell in the storyboard to the detail view controller that you want to activate.
If you really don't want to do it that way. Then another option would be to right-click and drag from your main view controller to the detail view controller to create a generic segue. Then name the segue. After that, you can call perform segue in your collectionView(collectionView, didSelectItemAtIndexPath) method. (Make sure you connect your view controller to your collection view as a delegate for this method to get called.)
Either way, you need to write a prepareForSegue(segue, sender) method in your main view controller to pass the selected item to the detail view controller.
Good luck on your new learning adventure!

how to implement multiple segues using dynamic cells in Xcode?

i have a table view with dynamic prototype cells divided into 2 different sections named "Forums" and "Threads". When i click on a tableview cell from the Forums section, i want to transit to the same page with a different data to display while if i click a cell from the Threads section, it should open a different scene. In short, two different types of transition segues from 2 different tableview sections)
Can anyone please help me with this?
Make sure the table view controller is embedded in a navigation controller.
Hook up two push segues to the table view controller (not to the cells).
Give those segues appropriate identifiers in the storyboard using the attributes inspector tab on each segue.
In -tableView didSelectRowAtIndexPath: add an if statement to detect which section the row tapped was in.
In the branches of the if statement, call -performSegueWithIdentifier on your controller using the identifier of the appropriate segue .
If you need to set up anything in the view controller you're seguing to from the table view, override prepareForSegue: sender: in the table view controller.
Very helpful, it worked for me, but I had to used BOTH didSelectRowAtIndexPath AND prepareForSegue, while I was reading in other posts that this is not very good.
I read that we need to use only the prepareForSegue if we need to set up any stuff...
Do you think that I should continue with both of them?
Thanks!

Custom View Controller display

I think this could be a simple question but I cannot seem to figure it out.
I have a screen which displays questions from an array. When the array has reached the end I want it to display another VC I have created. With all my other VC they are connected in the storyboard using segues between them. However, I only want this screen to display if I have reached the end of my array? Is what I am trying to doing making sense? Or does anyone know of any useful tutorials I could look at to figure it out for myself?
You can "manually" fire a seque with the performSegueWithIdentifier:sender: method using the segue identifier that you set on the segue in the storyboard. When you call that method the system sets up the right information and then executes prepareForSegue:sender: and then actually does the segue -- so the setup looks the same as an automatic segue trigger.
You can "manually" perform any segue with an identifier.
As to creating the segue to perform, you can create a segue from one View Controller to another in the storyboard (i.e. control-drag from the View Controller in the sidebar not from a button or a TableView cell or whatever). This sort of segue will have to be performed "manually".
Your call to do the "NextVCPlease" segue might look like:
if (lastQuestionDone) {
[self performSegueWithIdentifier:#"NextVCPlease" sender:whateverMakesSense]
}

Resources