Strange behaviour during mid-swipe back - ios

I'm experiencing a strange glitch where I can use the previous view controller before the top view controller has been dismissed.
At my main view controller I have a table view with the delegate function didSelectRowAtIndexPath. The function is below:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("commentsSegue", sender: self)
}
This works great, however if I am already showing this screen, I can select it again if I half-swipe back. This is sort of hard to explain but you can use a finger to swipe back and the other to select a cell on the main viewcontroller. This creates another segue to a new "commentsSegue". I can do this as many times as I like and it will action many segues.
I have tried to overcome this by using
if (self.presentingViewController?.presentedViewController == self) {
and also
if (self.navigationController?.topViewController.title == self.title) {
But both of these functions return the main viewcontroller as the active view controller instead of the "commentsSegue" controller.
How can I stop this behaviour from occurring?

On swipe create a Notification which check if your destination controller .isKindOfClass your current one. If not , make the segue.

Related

IOS swift UIBarButtonItem action

I've a table view with navigation controller embedded in. I've added a UIBarButtonItem (add) button. When I click this button it opens a new view where user enters the data and submits it (which makes a web service call) and returns back to the previous view. This navigation happens as shown below,
func addTapped(_ sender:UIBarButtonItem) {
print("Called Add")
let vc = (storyboard?.instantiateViewController( withIdentifier: "newNote")) as! newNoteVC
self.navigationController?.pushViewController(vc, animated: true)
}
And in new view I do following,
#IBAction func saveButton(_ sender: UIButton) {
if (self.noteDescription.text?.isEmpty)! {
print("Enter missing note description")
return
} else {
let desc = self.noteDescription.text
self.uploadNote(noteText: desc!, noteDate: self.dateInMilliseconds)
self.navigationController?.popViewController(animated: true)
}
}
This way a record gets saved and a view gets popped from the navigation controller stack but only thing I don't how to do is refresh the table view data in the parent view (where I might need to make a new http service call to read all the records).
I hope I'm able to explain the issue? Any help is appreciated.
As mentioned in the comments, making a service call just to update the tableview might be a overkill. However, if this is the business scenario which needs to be implemented, you can do the same in
func viewWillAppear
in the parent view controller. You can make the service call in this method and reload the table view with the data.
You would also need to check the overall navigation of the application as making service calls in viewWillAppear method is not a good approach as these gets called everytime the view is shown. For Ex: If coming back from a navigated view then also the method is called.

How can I test that a view controller has been presented?

I'm trying to write a unit test that a view controller is presented once a row is selected. The cell declaration doesn't work, because for some reason you can't call didSelectRow on the tableview. Also, I get an error that presentedVC is nil:
func testDidSelectNewsReportCalledWhenNewsReportSelected() {
var cell = tableView.didSelectRow(at: IndexPath(row: 0, section: 3), animated: false) //This line doesn't work
let presentedVC = controller.presentedViewController?.view
let newsReportVC = UIStoryboard(name:"News", bundle: Bundle.init(for: NewsViewController.self)).instantiateViewController(withIdentifier: "NewsReport") as! NewsReportViewController
XCTAssertEqual(newsReportVC, presentedVC)
}
Thanks.
In order to test this, you will have to look at the presentation stack (if you are presenting the view controller) or the navigation stack (if you are using a navigation controller.
Also, programmatically selecting a table view index path will not cause it to call it's delegate's didSelectRow method.
Don't bother testing to see if selecting a particular row causes the delegate's didSelect method to get called. Trust that Apple implemented their code correctly. All you need to test is that the table view has the correct delegate and that when didSelect is called, it does the right thing.
That said, testing view controller operation, especially presents and dismisses or pushes and pops is notoriously difficult and very slow. Don't do it. Move as much of your code as you can into the model layer and just test your models.

Segue to a New Instance of the Current View Controller

I have a storyboard view embedded in a navigation controller that displays a record from a database along with a link to view a related record. The related record needs to use the same view to display its data while still maintaining a navigation stack so the user can go back to the previous record. Keeping in mind that some data needs to be passed to the new viewController and the UI is composed of a tableView with each element in a row, how can this segue be accomplished?
Below is the view. If possible, please respond with any sample code in Swift.
With some inspiration from this answer and guidance by #Jassi, here is the final product:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let vc = storyboard?.instantiateViewControllerWithIdentifier("inventoryItemDetail") as InventoryDetail
vc.fmRecordId = item["inContainerRecordId"]! //this is the data which will be passed to the new vc
self.navigationController?.pushViewController(vc, animated: true)
}
An idea-
Give the view controller an identifier. And override the below function.
prepareForSegue
In that function instantiate the view controller using the identifier you have and then pass the necessary data to that controller. And push it on navigation controller.
I hope it will work.

Reusing a detail UIViewController involved in a storyboard segue

I have a master/detail application running on an iPad. When in landscape mode, I have both views up side-by-side. The right/detail view controller contains an MKMapView.
The issue is that when selecting a different table cell in the left/master view controller, and essentially re-performing the segue, the entire detail view controller is reinstantiated.
This means that the MKMapView I was using loses the user's position, and essentially starts from scratch, zooming in from the country scale to the street scale.
Is there a way to determine, prior to performing the segue, whether the detail view being displayed is already the one I want, and simply providing it new data and telling it to refresh?
For example:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
segueParkName = parkNames[indexPath.row]
self.performSegueWithIdentifier("showParkDetails", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showParkDetails" {
let controller = (segue.destinationViewController as UINavigationController).topViewController as ParkDetailsController
NSLog("Controller: \(controller)") // Different instance every time!
controller.parkName = segueParkName
}
}
I would like to either:
Somehow tell iOS that by the time prepareForSegue is reached, I'm okay with being provided a reused view controller, especially (!) if it's already displayed.
In the didSelectRowAtIndexPath method, perform a custom segue and do my own pushing. But I really like the idea of using the built-in system segues so I don't have to be specific about what I'm pushing and where. It seems more device-agnostic to use Show Detail (eg. Replace) than defining my own.
I think, in your first suggestion, it will be troublesome if not impossible to abandon the segue once you are in prepareForSegue. So I would go with your second option. But you don't need to trigger a segue at all, if the detail viewController you want is already in place. So rather than
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
segueParkName = parkNames[indexPath.row]
self.performSegueWithIdentifier("showParkDetails", sender: self)
}
you might have something like...
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
segueParkName = parkNames[indexPath.row]
self.detailViewController.parkName = segueParkName
}
This assumes that you already have a property detailViewController pointing to your detail ViewController. It also assumes that the detailViewController will always be the one you need - if necessary, check the detailViewController class to see whether it is the MKMapView you want. Finally, if setting parkName doesn't achieve everything you need (e.g. animating the change), then just implement a new method in your MkMapView and call that in place of setting parkName.
EDIT Just to expand on that, you can use:
if self.detailViewController.isKindOfClass(yourMKMapViewSubclass) {
self.detailViewController.parkName = segueParkName
}
to test whether detailViewController is indeed your MkMapView.
You can cancel a segue by implementing shouldPerformSegue however that is for the case where the park name is invalid for some reason, to prevent showing a view controller for an invalid park.
In this case the solution is use the reference to the detail controller in your master controller that the built-in master/detail template does for you. Then in prepareForSegue take the map from the old detail controller and put it on the new one.
As your app gets more complex it may no longer be suitable for the master to maintain a reference to the detail controller. For example, if you make a root controller that pushes a new master, then the master will not find the detail when the app is in portrait like the template app can. Thus in that case your class that implements the split controller delegate can also maintain the context for your master/detail (something that is initWithSplitViewController). By setting an owningContext param self on the splitViewController via a category in the init for this class, then you can access it from where you need to. E.g. setting the mapView on it from the master. And getting the mapView from it in the loadView of the detail.

iOS Swift - UITableView didselectrowatindexpath crashes

First of all, let me say that this is a noobish question so please bear with me here :D. I have a tableview embedded in a navigation control and it is my main view. There is an "Add" button where I can add "things" then when that is triggered it shows the added item on the table view and also switches back to the main view controller with the code below
#IBAction func buttonAddPost_Click(sender: UIButton) {
postMgr.addPost(titleBox.text, description: descriptionBox.text, postDate: date)
self.navigationController.popToRootViewControllerAnimated(true)
}
Again the above code takes me back to the main table view controller just fine.
But when I click on a cell to take me to a more detailed view, the app crashes. Here is the code
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
let listingPage = self.storyboard.instantiateViewControllerIdentifier("listingPage") as PostListingTableViewController
self.navigationController.pushViewController(listingPage, animated: true)
}
"listingPage" is the storyboard ID for the new view controller I'm trying to go to. I have used the above technique a few times somewhere else and worked just fine but I'm not sure what's wrong here.
Error I get:
Thread 1
0 swift_dynamicCast and it highlights 0x1d7150: pushl %ebp Thread 1: EXC_BREAKPOINT (code=EXC_I386_BPT)
1
2 Exchange.PostlistTableViewController ([app name].[class name]) and it highlights the didselectrowatindexpath
.
.
.
Please help...
KM
The exception tells you what the problem is - You are casting the view controller as PostListingTableViewController but the object type that is returned is PostlistTableViewController so the cast generates an exception.
You need to determine what the correct class name is and either update your storyboard or your didSelectRowAtIndexPath so that they are consistent - either Postlist or PostListing.

Resources