如何将动作从一个ViewController发送到另一个ViewController(一起活动)

时间:2018-09-12 11:34:54

标签: ios iphone swift google-maps

我知道这个问题被问过很多次了。但是在这里情况发生了变化。我正在使用MMParallaxView,看起来很棒。

但是现在,我要在这两个View控制器之间进行通信。让我告诉你,这个github代码可帮助我们制作类似于ios Map App的视图。我的意思是您可以拥有Map(作为第一视图控制器),并且您可以在第一视图控制器之上拥有一个位置视图控制器列表。就像地图应用一样。

现在,我想单击UITableView单元并想在地图上导航。为此,我知道如何捕获委托方法。而且我成功地获得了UItableView单元上的水龙头。但是如何将单击的项目数据发送到1stView Controller,以便它可以在Map上显示或标记所选区域。

我知道这也可以做到。但是如何?

3 个答案:

答案 0 :(得分:2)

要修改MMParallaxView的示例应用程序...


ChildBottomViewController.swift

将此内容添加到顶部(课程之外):

protocol CellTapDelegate {
    func didSelectRow(indexPath: IndexPath)
}

在课程开始时添加以下行:

var cellTapDelegate: CellTapDelegate?

didSelectRowAt函数更改为:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    DispatchQueue.main.async { [weak self] in

        self?.cellTapDelegate?.didSelectRow(indexPath: indexPath)
    }
}

MapViewController.swift

将类声明更改为:

class MapViewController: UIViewController, CellTapDelegate {

并添加此功能:

@objc func didSelectRow(indexPath: IndexPath) {
    print("Delegate got didSelectRow for: \(indexPath)")
}

然后,在SecondViewController.swift

viewDidLoad()的末尾添加此内容:

    var mapVC: MapViewController?
    var bottomVC: ChildBottomViewController?

    for vc in self.childViewControllers {
        if vc is MapViewController {
            mapVC = vc as? MapViewController
        } else if vc is ChildBottomViewController {
            bottomVC = vc as? ChildBottomViewController
        }
    }

    // if we found valid child view controllers, set the delegate
    if mapVC != nil && bottomVC != nil {
        bottomVC?.cellTapDelegate = mapVC
    }

现在,从“从底部向上滑动”表视图中选择一行,会将所选的indexPath发送到其委托-地图视图控制器。

答案 1 :(得分:0)

protocol CellTapDelegate {
  func didTapOnItem(obj: MyObject)
}
class MyViewControllerContainingTheTableView : UIViewcontroller {
weak var delegate: CellTapDelegate?
func tableView(_ tableView: UITableView, 
         didSelectRowAt indexPath: IndexPath) {
  let item = arrayOfObjects[indexpath.row]
  self.delegate.didTapOnItem(item)
}
}
//View Controller where you you will have the listner
class FirstViewController : UIViewController {
  func setupParalax() {
// you have to modify this according to your need
  let vc = MyViewControllerContainingTheTableView()
  vc.delegate = self

}
}
extension FirstViewController: CellTapDelegate {
  func didTapOnItem(obj: MyObject) {
  // you have your required object here
}
}

答案 2 :(得分:0)

应该可以通过实现自己的委托来实现,但是我对MMParrallaxView的了解是,只有一个视图控制器可以显示两个视图,这意味着该视图控制器应该能够按原样在两个视图之间传递内容。在这种情况下,视图控制器将实现表视图委托方法,并将表视图添加到底部视图。然后将地图添加到顶视图。这样一来,您就可以捕获表格视图单元格的选择,并相应地在顶视图中更新地图。

基于我认为您正在使用的结构的示例:

public class FirstViewController {

    public let parallaxView = MMParallaxView()
    private var secondViewController: SecondViewController
    private var thirdViewController: ThirdViewController

    override open func viewDidLoad() {
        secondViewController = SecondViewController()
        thirdViewController = ThirdViewController()
        super.viewDidLoad()
        self.view.addSubview(parallaxView)
        thirdViewController.delegate = secondViewController

        parallaxView.parallaxTopView = secondViewController.view
        self.addChildViewController(secondViewController)
        secondViewController.didMove(toParentViewController: self)

        parallaxView.parallaxBottomView = thirdViewController.view
        self.addChildViewController(thirdViewController)
        thirdViewController.didMove(toParentViewController: self)
    }
}

public class SecondViewController: ThirdViewControllerDelegate {

    public func updateMap() {
        // Update map
    }
}

public class ThirdViewController: UITableViewDelegate {

    weak var delegate ThirdViewControllerDelegate?

    private var table: UITableView = UITableView()

    open override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        delegate?.updateMap()
    }

}

protocol ThirdViewControllerDelegate: class {
    public func updateMap() {}
}

如果通过Storyboard实例化了视图控制器,则可以尝试以下操作:

public class MasterViewController {

    var secondViewController: SecondViewController?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.performSegue(withIdentifier: "MMParallaxTop", sender: nil)
        self.performSegue(withIdentifier: "MMParallaxBottom", sender: nil)
    }

    override open func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let secondVC = segue.destination as? SecondViewController {
            secondViewController = secondVC
        }
        if let thirdVC = segue.destination as? ThirdViewController, let second = secondViewController {
            thirdVC.delegate = second
        }
    }
}