ios - 我在第二个 View 上有一个 UISwitch,如何控制切换方法

标签 ios swift uiswitch

我想要开关控制用户的轨迹记录,但我的开关在第二个 View 上。是否需要发送通知或使用协议(protocol)委托(delegate)?

第一次查看

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)

}
}

第二 View

class SencodTableViewController: UITableViewController {

@IBAction func drawRouteSwitch(_ sender: UISwitch) {

    if sender.isOn == true {

    } else {

    }

}

我想在第二个页面切换时控制drawRoute函数,该怎么做?

最佳答案

您将需要使用协议(protocol)委托(delegate)。

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

protocol SwitchToggleDelegate{
    func didToggleSwitch(state: Bool)
}

在第二个 View 中(使用开关): 引用委托(delegate)(调用你想要的方法)

var switchDelegate: SwitchToggleDelegate? = nil

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

class View2: UIViewController {

    var switchDelegate: SwitchToggleDelegate? = nil

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

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

在您的第一个 View 中:

订阅您的新委托(delegate)并添加所需的方法:

class View1: UIViewController, SwitchToggleDelegate { }

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

您还需要将第一个 View 的委托(delegate)设置为等于第二个 View 。由于我为此使用容器 View ,因此我使用 prepare for segue:

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

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

关于ios - 我在第二个 View 上有一个 UISwitch,如何控制切换方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51042967/

相关文章:

ios - 向 UIView 添加带宽度的边框在外面显示小背景

ios - 如何通过特定的力量旋转 Sprite 节点,例如指尖陀螺

iphone - UI 在 iOS 5 和 iOS 4.3 上看起来不同

iOS7 UISwitch 其事件 ValueChanged : Calling continuously is this Bug or what. .?

objective-c - 谷歌加 iOS SDK : how to get logged in user email?

ios - 如何从类中为 EnvironmentObject 设置值?

ios - Bugsnag:升级到版本 5 时缺少 mergeWith 函数

ios - tableviewcell 的附件 View 中的 UISwitch,通过选择器传递参数以使选择器函数看到 indexpath.row?

ios - 更改 fontWithName 中的大小值没有明显效果

javascript - 无法读取未定义的属性 'messageHandlers'