【问题标题】:I have a UISwitch on the secondView, how do I control the switch method我在secondView上有一个UISwitch,如何控制切换方法
【发布时间】:2018-06-26 12:28:51
【问题描述】:

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

第一视图

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)

}
}

秒视图

class SencodTableViewController: UITableViewController {

@IBAction func drawRouteSwitch(_ sender: UISwitch) {

    if sender.isOn == true {

    } else {

    }

}

我想从第二个页面切换控制drawRoute func,怎么做?

【问题讨论】:

  • 您需要使用协议代表来“听到”交换机引发的事件。在这种情况下你不应该使用通知,这就是委托的设计目的,发送事件/消息/foo。
  • 你能用代码演示一下吗?
  • 而不是仅仅解决手头的问题。你应该明白。使用代表。你可以阅读如何在swiftwithsadiq.wordpress.com/2017/07/20/delegate-in-swift

标签: ios swift uiswitch


【解决方案1】:

您将需要使用协议委托。

首先定义一个委托(它可以有任意数量的函数)

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),并且订阅此委托方法的任何内容(例如您的第一个视图)都会听到此委托回调并执行您为委托准备的任何代码。

【讨论】:

  • 代表非常有用,这是一个很好的视频,通过设置它们youtube.com/watch?v=DBWu6TnhLeY。您可以将它们用于网络调用等,它们对于外观等模式至关重要。
  • 我可以在didUpdateLocations func中问我应该做什么
  • 当我把开关转回第二页时他已经关闭了
  • @Josh 您是否尝试过在独立项目中实现此代码?它将帮助您了解代表的工作方式。在你的功能中,你可以做任何你想做的事,这取决于你!
【解决方案2】:

您确实有几个选项,例如NSNotification、委托,甚至是单例(不建议在这种情况下使用)。我最喜欢在一对特定的视图控制器之间传递信息的方法是委托。这很简单。无需通过系统的通知中心。而且很具体:只有一个监听器和一个通知器。

创建以下委托:

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

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

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。转到您的故事板,单击 segue(您的 VC 之间的链接节点),然后更新下图中显示的字段:

Segue ID update

在你的第一个 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)
    }

}

【讨论】:

  • 我想在哪里实现 func switchValueDidChange @Sales Account
  • 您将扩展您的第一个 VC 以实现响应。查看更新后的答案。
  • 如果您发现答案有帮助,请考虑为答案投票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多