gpt4 book ai didi

ios - 如何将 selectedSegmentIndex 的值传递给另一个 viewController?

转载 作者:行者123 更新时间:2023-11-29 11:31:36 25 4
gpt4 key购买 nike

我有一个 PopUpViewController,当在主 ViewController 中按下按钮时,它会弹出。我在三个不同的 ViewController 上有一个 speedDisplay (UILabel),我希望能够在 MPH 和 KMH 之间切换输出,但我不知道该怎么做。我在 PopUpViewController 中有一个 segmentControl,我正在尝试实现它以实现这一点。有人可以帮我从这里出去吗?

import UIKit

public enum Speed {
case KMH
case MPH
}
protocol PopUpViewControllerDelegate: class {

func selectedSpeedType(_ type: Speed)
}

class PopUpViewController: UIViewController, UIGestureRecognizerDelegate {

weak var delegate: PopUpViewControllerDelegate?


@IBOutlet weak var altitudeSegment: UISegmentedControl!
@IBOutlet weak var tempSegment: UISegmentedControl!
@IBOutlet weak var speedSC: UISegmentedControl!

@IBOutlet weak var doneButton: UIButton!
@IBOutlet weak var settingsView: UIView!



override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.8)

settingsView.layer.masksToBounds = true
settingsView.layer.cornerRadius = 8

doneButton.layer.masksToBounds = true
doneButton.layer.cornerRadius = 8

let items = ["MPH", "KMH"]
let segmentedControl = UISegmentedControl.init(items: items)
segmentedControl.selectedSegmentIndex = 0
segmentedControl.addTarget(self, action: #selector(speedChoice(_sender:)), for: UIControl.Event.valueChanged)
self.view.addSubview(segmentedControl)

}

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

@IBAction func speedChoice(_ sender: UISegmentedControl) {

let index = sender.selectedSegmentIndex

switch index {
case 0:
// Needs to euqal MPH to use on speedDisplay in another viewController
case 1:
// Needs to euqal KMH to use on speedDisplay in another viewController
default:
break
}
}

这是我在 ViewController 上的速度函数:

override func viewDidLoad() {
super.viewDidLoad()
getCurrentDateTime()
self.locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
locationManager.startUpdatingHeading()
locationManager.headingFilter = 5
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
map.mapType = MKMapType.standard
let location = locations[0]
let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let span = MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)
let region = MKCoordinateRegion(center: myLocation, span: span)
map.setRegion(region, animated: true)
self.map.showsUserLocation = true

let speed = location.speed
if speed < 2 {
self.speedDisplay.text = "0.0"
}
else {
if speedType == .MPH {
self.speedDisplay.text = String(format: "%.1f", speed * 2.236936284, "MPH")
speedTypeLabel.text = "MPH"
}
else {
self.speedDisplay.text = String(format: "%.1f", speed * 3.6, "KMH")
speedTypeLabel.text = "KMH"
}
}
extension ViewController: PopUpViewControllerDelegate {
func selectedSpeedType(_ type: Speed) {
speedType = type
}
}

以及弹出窗口的代码:

@IBAction func showPopup(_ sender: Any) {

let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbPopUpID") as! PopUpViewController
self.addChild(popOverVC)
popOverVC.view.frame = self.view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParent: self)

}

}

我有以下用于保存 segmentedController 的内容:我相信我遗漏了一些东西,因为每次我选择一个段时它都会返回到段 0。它打印出来就像它正在工作但它没有保存选择。有什么想法吗?

@IBAction func closePopUp(_ sender: UIButton) {
self.view.removeFromSuperview()
}

最佳答案

使用 delegate 机制将数据从一个 ViewController 发送回另一个。

PopUpViewController 内部

import UIKit

public enum Speed {
case KMPH
case MPH
}
protocol PopUpViewControllerDelegate: class {

func selectedSpeedType(_ type: Speed)
}

class PopUpViewController: UIViewController, UIGestureRecognizerDelegate {

weak var delegate: PopUpViewControllerDelegate?

//This is global variable (internal protection level) and can be get/set from outside of the class.
var speedType = Speed.KMPH //New line

@IBOutlet weak var altitudeSegment: UISegmentedControl!

......
......

override func viewDidLoad() {
super.viewDidLoad()

.......

//We can select the segment control with corresponding value even popover is removed from the ViewController.
altitudeSegment.selectedSegmentIndex = speedType.hashValue //New line
}

@IBAction func speedChoice(_ sender: Any) {

let segmentControl = sender as! UISegmentedControl
if segmentControl.selectedSegmentIndex == 0 {
delegate?.selectedSpeedType(.KMPH)
} else {
delegate?.selectedSpeedType(.MPH)
}
}
}

YourViewController 中:

class ViewController: UIViewController {

var speedType: Speed! = .KMPH
var popOverVC: PopUpViewController!

override func viewDidLoad() {
super.viewDidLoad()

popVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbPopUpID") as! PopUpViewController
popVC.delegate = self
}

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

.....

self.speedDisplay.text = "\(String(format: "%.1f", speed * 2.236936284)) \((speedType == .KMPH) ? "KMPH" : "MPH")"
}

@IBAction func showPopup(_ sender: Any) {

self.addChild(popOverVC)
popOverVC.view.frame = self.view.frame
//Assign the speedType to selected value back in PopOverViewController, and update the segmentControl to new value.
popOverVC.speedType = speedType //New line
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParent: self)
}
}

extension ViewController: PopUpViewControllerDelegate {

func selectedSpeedType(_ type: Speed) {

speedType = type
}
}

关于ios - 如何将 selectedSegmentIndex 的值传递给另一个 viewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53001111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com