简体   繁体   中英

Swift iOS: Drag & Drop between two Containers fails

I'm entering the world of Swift, stepping forward slowly...

What I'm trying to have is an UIViewController with two Container Views, each of them having its own UIViewController with an UITableView, where it is possible to drag a table entry from the right container's table, and drop it on the left container's table. The parent UIViewController shall care about everything around that drag'n'drop operation.

I therefore added a Long Press Gesture Recognizer to the UIViewController and wired it accordingly:

@IBAction func longPress(sender: UILongPressGestureRecognizer) {

    switch(sender.state) {
    case UIGestureRecognizerState.Began: startDragDrop(sender)
    case UIGestureRecognizerState.Ended: endDragDrop(sender)
    default: return
    }

}

where

func startDragDrop(sender: UILongPressGestureRecognizer) {

    var startPoint: CGPoint = sender.locationInView(self.structureElementContainer!.tableView)

    var indexPath = self.structureElementContainer.tableView.indexPathForRowAtPoint(startPoint)!

    println("startPoint: \(startPoint.x) \(startPoint.y)")
    println("\(indexPath.section) \(indexPath.row)")
}

When long-clicking on a table entry, there is a crash in the "var startPoint" line:

2015-03-20 15:45:59.822 xxx.20[10274:18100886] -[UIView tableView]: unrecognized selector sent to instance 0x7a14fa90
2015-03-20 15:45:59.828 xxx.20[10274:18100886] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView tableView]: unrecognized selector sent to instance 0x7a14fa90'

What's going wrong here?

Thanks a lot for your support

[Added 23-mar-2015] I tried now to follow Mundi's hint, but without success up to now.

This is the view controller fragment of the parent, where I added outlets for the two containers:

class xxxDetails: UIViewController {

@IBOutlet weak var viewTitle: UILabel!

@IBOutlet weak var aaaElementsContainer: UIView!
@IBOutlet weak var bbbStructureContainer: UIView!

@IBOutlet var gestureRecognizer: UILongPressGestureRecognizer!

var structureElementsController: StructureElementsController!

override func viewDidLoad() {
    super.viewDidLoad()

}

The containers embed a UITableViewController each:

class aaaElementsController: UITableViewController {...}
class bbbElementsController: UITableViewController {...}

I tried to create an Outlet for those controllers in the parent UIViewController, without success.

I tried to create a property in the UITableViewController which refers to itself, to be able to access it from the parent, without success.

It seems that I'm possibly following a wrong approach. So the question is now for me: When embedding two child UITableViewControllers via ContainerViews in a parent UIView, how do I access those children correctly, so that I'm able to have gesture recognition in the parent which is able to follow a drag&drop from the one child to the other?

Your self.structureElementContainer does not have a tableView property. Presumably the controller of that view has one. You have to refactor to have the main view keep a reference to the controllers, not the views.

Well, Mundi's response did not solve my problem directly, but it put me on the track of a sequence of information that led to the solution... ;-)

I was pretty surprised that things may be so simple. I was stuck on the fact that I did not get a valid handle to the subview controllers. However, to get these handles, you just have to care about the embed segue during viewDidLoad:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "aaaElementsSegue" {

        aaaElementsController = segue.destinationViewController as AaaElementsController

    }

}

After that, the aaaElementsController is valid and allows access to the table view in the child view.

And, as this is true for the second child view, I'm now able to handle the drag&drop between those two child views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM