繁体   English   中英

我在secondView上有一个UISwitch,如何控制switch方法

[英]I have a UISwitch on the secondView, how do I control the switch method

我希望该开关控制用户的跟踪记录,但是我的开关位于secondView上。 是否需要发送通知或使用协议委托?

firstView

class MapViewController: UIViewController {

@IBOutlet weak var mainMapView: MKMapView!

  func drawRoute(locationArray: [CLLocation]) {
    if locationArray.count > 1 {
        let destinationLocIndex = (locationArray.count) - 1
        let startLocIndex = (locationArray.count) - 2

        let destinationloc = locationArray[destinationLocIndex].coordinate
        let startLoc = locationArray[startLocIndex].coordinate

        let routeArray = [startLoc, destinationloc]

        let geodesicLine = MKGeodesicPolyline(coordinates: routeArray, count: routeArray.count)
        mainMapView.add(geodesicLine, level: .aboveRoads)

    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    // show current user's location
    setLocation(nowlocation: locations.last!)

    //I want to control it from the switch on the second page
    drawRoute(locationArray: userLocation)

}
}

seconView

class SencodTableViewController: UITableViewController {

@IBAction func drawRouteSwitch(_ sender: UISwitch) {

    if sender.isOn == true {

    } else {

    }

}

我想从第二个页面开关控制drawRoute函数,该怎么办?

您确实有两种选择,例如NSNotification ,委托甚至单例(不建议用于此方案)。 我最喜欢的在一对特定视图控制器之间传递信息的方法是通过委派。 这很简单。 无需通过系统的通知中心。 这很具体:只有一个侦听器和一个通知器。

创建以下委托:

class SencodTableViewControllerDelegate: class {
    func switchValueDidChange(_ viewController: SencodTableViewController, isSwitchOn: Bool);
}

您在prepare(for:)方法中将第一个VC设置为“侦听器”。

let SegueToSecondViewID = "segueToSecondViewID"

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == SegueToSecondViewID {
        if let secondVC = segue.destination as? SencodTableViewController {
            secondVC.delegate = self
        }
    } else {
        super.prepare(for: segue, sender: sender)
    }
}

您还应确保在情节SegueToSecondViewID以及在情节SegueToSecondViewID中定义SegueToSecondViewID 转到情节提要,单击segue(VC之间的链接节点),然后更新下图所示的字段:

Segue ID更新

在您的第一个VC中,还应通过遵循SencodTableViewControllerDelegate协议来实现“响应”方法:

extension MapViewController: SencodTableViewControllerDelegate {
    func switchValueDidChange(_ viewController: SencodTableViewController, isSwitchOn: Bool) {
        // Do what you want with drawRoute
    }
}

然后,当您要发送开机消息时:

class SencodTableViewController: UITableViewController {

    weak var delegate: SencodTableViewControllerDelegate?
    ...

    @IBAction func drawRouteSwitch(_ sender: UISwitch) {
        delegate.switchValueDidChange(self, sender.isOn)
    }

}

您将需要使用协议代表。

首先定义一个委托(它可以具有许多功能)

protocol SwitchToggleDelegate{
    func didToggleSwitch(state: Bool)
}

在第二个视图中(使用开关):引用委托(以调用所需的方法)

var switchDelegate: SwitchToggleDelegate? = nil

并从开关的操作中调用委托方法:(我已经包括了完整的代码)

class View2: UIViewController {

    var switchDelegate: SwitchToggleDelegate? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func OnSwitchToggled(_ sender: UISwitch) {
        switchDelegate?.didToggleSwitch(state: sender.isOn)
    }
}

在您的第一个视图中:

订阅您的新代表并添加所需的方法:

class View1: UIViewController, SwitchToggleDelegate { }

func didToggleSwitch(state: Bool){
        //Do Something
        labelOne.text = "Switch is \(state)"
    }

您还需要将第一个视图的委托设置为等于第二个视图。 由于我为此使用了容器视图,因此我要使用“准备segue”:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "embedded"{
        let view2: View2 = segue.destination as! View2
        view2.switchDelegate = self
    }
}

现在,切换开关时,将func didToggleSwitch(state: Bool)委托方法func didToggleSwitch(state: Bool) ,并且预订该委托方法的任何内容(例如您的第一个视图)都将听到此委托回调并执行为委托准备的任何代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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