使用未解析的标识符' showAlert'迅速

时间:2016-11-12 17:24:48

标签: swift xcode identifier

我是Swift中的新手,我已经堆叠了这段代码,并且每次都返回给我"使用未解析的标识符' showAlert'用户输入区域和用户退出区域:

    func setupData() {
        // 1. check if system can monitor regions
        if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {

            // 2. region data
            let title = "Primo punto"
            let coordinate = CLLocationCoordinate2DMake(38.121973, 13.360855)
            let regionRadius = 300.0

            // 3. setup region
            let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate.latitude,
                                                                         longitude: coordinate.longitude), radius: regionRadius, identifier: title)
            locationManager.startMonitoring(for: region)

            // 4. setup annotation
            let restaurantAnnotation = MKPointAnnotation()
            restaurantAnnotation.coordinate = coordinate;
            restaurantAnnotation.title = "\(title)";
            mapView.addAnnotation(restaurantAnnotation)

            // 5. setup circle
            let circle = MKCircle(center: coordinate, radius: regionRadius)
            mapView.add(circle)
        }
        else {
            print("System can't track regions")
        }
    }

    // 6. draw circle
    func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
        let circleRenderer = MKCircleRenderer(overlay: overlay)
        circleRenderer.strokeColor = UIColor.red
        circleRenderer.lineWidth = 1.0
        return circleRenderer
    }

    // 1. user enter region
    func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
        showAlert("enter \(region.identifier)")
    }

    // 2. user exit region
    @nonobjc func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
        showAlert("exit \(region.identifier)")
    }
}

1 个答案:

答案 0 :(得分:0)

我不知道你是否有'showAlert()'函数,但是如果你试图提醒用户那么你可以这样做:

//Create alert
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)

// Add action buttons to the alert
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))

// Present the alert to the view
self.present(alert, animated: true, completion: nil)

如果你想把它作为一个函数,那么只需创建一个名为show alerts并将自己的参数添加到函数中。像这样:

func showAlert(Title: String, Message: String) {
    let alert = UIAlertController(title: Title, message: Message, preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
    self.present(alert, animated: true, completion: nil)
}