Passing data from the second view into the first view controller - xcode

I want to know how do you pass data, like values, from a second view controller into the first view controller. I did a simple segue from the first to the second. I just don't know how to do it backwards.
I have a view on my first controller, and a view on the second one. I want to pass the changes from the second view into the view on the first controller when I click on a custom back button I made. How do I go about doing that?
I know it has something to do with unwind segue, but everything I look up is very unclear and confusing.

To do it backwards, you could do it with delegate or blocks like what #Rob linked in the comments.
You can also do a unwind segue like what you mention. Just like a simple segue, unwind segue could help you pass data from the second view controller to first view controller in prepareForSegue.
Example you have a ViewControllerA that segue to the ViewControllerB. This time you want to setup the unwind segue for ViewControllerB to ViewControllerA. Before setting up on storyboard, you will need to setup the #IBAction in ViewControllerA the UIViewController you want to unwind to not ViewControllerB. Something like this:
#IBAction func cancelToViewControllerA(segue:UIStoryboardSegue) {
}
In storyboard, at the ViewControllerB control drag the button that will call the unwind segue to the exit and select cancelToViewControllerA. Then give it a name for the unwind segue. In ViewControllerB override prepareForSegue like what you did for passing data forward to set the data for the unwind segue.

Related

Swift back button with segue show not showing

So I tried everything with showing up the back button but it just won't work
This is how i arrange my View Controllers Used segue show from view controller to site view controller
And here is the site view controller that isn't showing back button
I even tried adding NavigationBar and a button with this function
self.navigationController.popToRootViewControllerAnimated(true)
Also failed
Update:
I tried this in both view controllers it's returning nil
in viewDidAppear and viewWillAppear
print(self.navigationController?.restorationIdentifier)
Once I got the same problem, when I was try to use view property of my UIViewController before it loaded properly. This caused problems with loading my navigation bar items. In fact, you should not touch view property until viewDidLoad method is called. Check, maybe it is your case, maybe you use view property in prepareForSegue method or in observers?

How do you put and control a checkbox in an NSTableView?

I am looking for a way of programmatically putting a checkbox in the first column of an NSTableView. I want to do it in Swift. I need to be able to read the values of all the buttons and set the button when someone clicks the row.
Is there an easy way to do this? I can't find it...
You need the table view to be view-based, not cell-based. The easiest way IMHO is to put the checkbox into the first column of your table view with the interface builder. It should be a sibling of your text field. I assume you want the first column to just contain a checkbox, and nothing else. In this case you should hide the textfield, so that it does not interfere with the checkbox (click the "hidden" checkbox in interface builder for the textfield).
Option 1: create a subclass of NSTableCellView, name it MyTableCellViewWithCheckbox, and give it a #property (nonatomic,strong) NSButton *checkbox; Change the custom class field from NSTableCellView to MyTableCellViewWithCheckbox for the first column and connect the outlet.
Option 2: (I haven't tested it yet, but I think it should work.) Set the tag of the checkbox to 13. To retrieve the checkbox later from the table view cell, you can use the viewWithTag method.
Next, you need to attach an action to your checkbox, so you should create an IBAction onto your view controller (not onto MyTableCellViewWithCheckbox.) In the action-method you can use the tableviews rowForView method to get the row index of the checkbox the user just tapped. The new state can be received directly from the sender of the action (it is an NSButton).
Also, don't forget that awakeFromNib may be called multiple times in your view controller if you have a table view in it - once for each table view cell you instantiate (this took me some time to figure out.)
I was able to do this by using a Cell-based NSTableView. Make the TableView look like this:
Set the tableColumn to look like this:
Set the buttonView to look like this:
Set the checkbox by having func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn?, row: Int) -> AnyObject? return 1 for a check and 0 for a non-check.

Getting nil when preparing for segue to navigation controller

I have found a few other questions about this same topic, so this is technically a repost, however the solutions provided there are not helping me at all. The solutions that have been suggested, which seem to work for the other users, are already present in my code.
Here is the code that is causing me problems.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
let navView = segue.destinationViewController as UINavigationController
let destinationView = navView.topViewController as DetailViewController
And here is the error I am getting:
fatal error: unexpectedly found nil while unwrapping an Optional value
The error is thrown on the error where I am accessing navView.topViewController.
I have a table view segueing to a navigation controller, which then segues to my own custom view controller under it. I want to pass data to my custom view controller from the table view.
I had it working fine earlier today before adding the navigation controller in between, passing data directly from the table view to the navigation controller. Adding the navigation controller in between has broken things.
Are there any suggestions as to why I am having this problem? The only thing I can think is that the DetailViewController hasn't instantiated yet since it is two levels deep now. But then if that is the case, I don't know why it seems to be working for other people.
I have a table view segueing to a navigation controller, which then segues to my own custom view controller under it.
I believe that is your problem. The connection from the Navigation Controller to your Detail View Controller isn't a normal segue. It should be a Relationship Segue.
Click on the connection between the Navigation Controller and the Detail View Controller. Press delete.
Control-drag from the Navigation Controller to the Detail View Controller and select root view controller from the pop up.
Now navView.topViewController will be non-nil.

iOS8 UISplitViewController: How to switch to master view in compact width window?

My root view controller is an UISplitViewController, which has a UITableViewController as master view controller. On iPhone (compact width), it looks like a UINavigationController.
Tap a cell to show detail view controller
Tapping the trash button would delete the current note. My problem is how to go back to the master view after that? Since it's an UISplitViewController, it can't pop the current view controller as UINavigationController does.
I had a similar problem and finally found a solution. As I understand it, when in compact width, the detail navigation controller becomes a view controller of the master navigation controller. So all you have to do is:
Determine if only one view is present by checking the split view controller's collapsed property. If it isn't collapsed (e.g. on iPad), you're already showing the table view in addition to the detail view.
If it is collapsed (e.g. on iPhone) get a reference to the master navigation controller via the detail navigation controller and have it pop to its root view controller which, in this case the your table view controller.
This is the code I use in my detail view controller. In your case I think you just need to add this code to the button action in your detail view controller:
if splitViewController!.collapsed {
let detailNavController = parentViewController as UINavigationController!
let masterNavController = detailNavController.parentViewController as UINavigationController!
masterNavController.popToRootViewControllerAnimated(true)
}
Good luck!

Calling a function when a view controller is dismissed (xcode/iOS)

I have a main view controller. When a specific button is pressed, I present another view controller. When the second view controller is dismissed, I want to call a function in the main view controller. Originally I thought I could just use the completion handler for presentviewcontroller, but now I'm thinking that is not the way to go. Any tips on how I can accomplish this? I'm pretty new to this, so any help would be appreciated!
Assuming your main view controller is the second view controller's parent, you can access the method with:
[[secondViewController parentViewController] method];
Beware -- if you are using a navigation controller, the parent view controller will be the navigation controller. In this case, you will have to query the navigation controller's stack.
Good luck!

Resources