Getting nil when preparing for segue to navigation controller - xcode

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.

Related

Passing data from the second view into the first view controller

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.

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?

Cannot connect IBAction to table view from xib

I am using this open source framework in swift https://github.com/uacaps/PageMenu for my view controller.
I am using xib for the tableview cells and the tableview as shown in the demo projects.
However, when I go to connect my buttons from the tableview cell to the table view controller Xcode will not let me. I am working on the NotificationTableViewCell and NotificationTableVC files.
I have set up the project properly and can get the cell up and running as well as the project itself. However if I can't get the buttons to connect I cannot add the needed functionality.
The demo projects also have this behaviour when I cloned some from the repository. I looked through the issues and no one seemed to mention anything about connecting IBActions to the view, so I did, but I assume there is something I am missing.
I have attached screen shots to help you see my issue. I need to access the cellForRowIndexPath as well.
My xib is given the class of my table view cell which still wont allow me to interact with the tableview controller.
Okay, so I have figured out a working solution. Thanks to Gurtej Singh.
Here is the code:
1) I added this line in the cellForRowIndexPath
cell.mybutton.addTarget(self, action: "declineRequest:", forControlEvents: .TouchUpInside)
2) Now I am able to access the button if I write a function called declineRequest
Since I want to delete the cell based on its data I use this function and add the Parse query logic.
func declineRequest(sender: UIButton) {
var cell: NotificationsTableViewCell = sender.superview!.superview as! NotificationsTableViewCell
.... }
I don't think you will be able to connect a table view cell element to a table view controller class via IB (and from what I know, it's not correct to do so as well, because there will be multiple cells and re-usability).
Connect the element from the IB to your Cell class (.h/.m), create one if you don't have it , like NotificationTableViewCell.m, and then access the connected element from the delegate methods inside your Table View controller class.
Hope this helps.

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