ios - UIAlertController 按钮功能不起作用

标签 ios swift function uialertcontroller

let location =  CLLocationCoordinate2D(latitude: 32.075300, longitude: 34.782563)

    @IBAction func DirectionsTolocationButton(_ sender: Any) {
        // Create the AlertController and add its actions like button in ActionSheet
        let ActionSheet = UIAlertController(title: "Please Select A Navigation Service.", message: nil, preferredStyle: .actionSheet)

        let AppleMapsButton = UIAlertAction(title: "Apple Maps", style: .default) { action -> Void in

            let destinationName = (self.barNameTemplate ) 
            self.openMapsAppWithDirections(to: self.CoordinatesTemplate, destinationName: destinationName)
            print("Apple Map Chosen!")

        }
        let WazeButton = UIAlertAction(title: "Waze", style: .default) { action -> Void in

            func openWaze(location : CLLocationCoordinate2D) {
                if UIApplication.shared.canOpenURL(URL(string: "waze://")!) {
                    // Waze is installed. Launch Waze and start navigation
                    let urlStr: String = "waze://?ll=\(location.latitude),\(location.longitude)&navigate=yes"
                    UIApplication.shared.openURL(URL(string: urlStr)!)
                }
                else {
                    // Waze is not installed. Launch AppStore to install Waze app
                    UIApplication.shared.openURL(URL(string: "http://itunes.apple.com/us/app/id323229106")!)
                }
            }
            print("Waze Chosen!")
        }

当我选择 WazeButton 时,没有任何反应......正如 Waze 在他们的 API 中所说,iv 也将其添加到我的 Plist 中:

<key>LSApplicationQueriesSchemes</key>
<array>
        <string>waze</string>
</array>

为什么它对我不起作用?

最佳答案

您需要在函数之外添加函数并按如下方式调用。

func openWaze(location : CLLocationCoordinate2D) {
                if UIApplication.shared.canOpenURL(URL(string: "waze://")!) {
                    // Waze is installed. Launch Waze and start navigation
                    let urlStr: String = "waze://?ll=\(location.latitude),\(location.longitude)&navigate=yes"
                    UIApplication.shared.openURL(URL(string: urlStr)!)
                }
                else {
                    // Waze is not installed. Launch AppStore to install Waze app
                    UIApplication.shared.openURL(URL(string: "http://itunes.apple.com/us/app/id323229106")!)
                }
            }

如下调用

let WazeButton = UIAlertAction(title: "Waze", style: .default) { action -> Void in

            self. openWaze(location : your corrdinates)// call here your function
            print("Waze Chosen!")
        }

关于ios - UIAlertController 按钮功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44999453/

相关文章:

ios - 定义本地 HTML 文件的位置

ios - 如何使用特定的宽度和高度 View 设置约束

swift - 如何使用 swift 检测单元测试中的崩溃

swift - TabView 在切换选项卡时重置导航堆栈

javascript - 移动设备上的 jquery .click() 函数

ios - 在 ios 中的表格单元格中单击按钮更改图像

ios - Swift 1.2 中未正确实现随机播放功能

ios - 在线向结构添加数据时出错(Swift)

javascript - 尝试将文本字段中输入的两个数字相乘并使用 jquery 提醒答案

swift - 看起来返回键对我的方法不起作用