gpt4 book ai didi

swift - 如何在 Swift 中更改 UIBarButtonItem 事件的功能

转载 作者:行者123 更新时间:2023-11-30 11:42:40 24 4
gpt4 key购买 nike

我一直在关注 Let's Build That App 中的这个 Youtube 教程,以使用 Google Maps SDK 实现分步导航系统。我希望能够将以编程方式生成的“下一个”UIBarButtonItem 更改为最后一个 map 位置上的“完成”按钮,并使用它返回到上一个 ViewController

使用 Swift 进行此操作的正确方法是什么?

https://www.youtube.com/watch?v=8wPjCdDn2wo

我的代码:

    navigationItem.rightBarButtonItem = UIBarButtonItem(
title: "Next",
style: .plain,
target: self,
action: #selector(nextLocation)
)
}

func nextLocation() {

if currentDestination == nil {
currentDestination = destinations.first


}
else {

if let index = destinations.index(of: currentDestination!), index < destinations.count - 1{
currentDestination = destinations[index + 1]

}
}

setMapCamera()
}

private func setMapCamera() {
CATransaction.begin()
CATransaction.setValue(2, forKey: kCATransactionAnimationDuration)
mapView?.animate(to: GMSCameraPosition.camera(withTarget: currentDestination!.location, zoom: currentDestination!.zoom))

CATransaction.commit()
let marker = GMSMarker(position: currentDestination!.location)
marker.title = currentDestination?.name
marker.map = mapView

}

/* My incorrect code */

func lastLocation() {

if currentDestination == destinations.last {
navigationItem.rightBarButtonItem = UIBarButtonItem(
title: "Finish",
style: .plain,
target: "myItinerary",
action: #selector(lastLocation)
)
}
}

最佳答案

我希望这会对您有所帮助:第一个:检测最后一个位置或在最后一个位置后单击栏按钮,将栏按钮标签更改为“完成”。然后在单击“完成”按钮后再次将操作设置为“下一步”,并将目标计数器设置为第一个值。

func next() {

if currentDestination == nil {
currentDestination = destinations.first
} else {
if let index = destinations.index(of: currentDestination!), index < destinations.count - 1 {
currentDestination = destinations[index + 1]
}
else {
// After last Location change button title to "Done"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(lastLocation))
}

}
setMapCamera()
}

func lastLocation() {
currentDestination = destinations.first // Set destinations counter to First value
// Change Title of Bar Button
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(ViewController.next as (ViewController) -> () -> ()))
}

如果您想向后移动位置(反向),则:

func lastLocation() {
currentDestination = destinations.last
destinations = destinations.reversed()
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(ViewController.next as (ViewController) -> () -> ()))
}

关于swift - 如何在 Swift 中更改 UIBarButtonItem 事件的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49170132/

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