标签云

微信群

扫码加入我们

WeChat QR Code

I am making a small map type application. I wish to display a popover menu once the button 'morestuff' is clicked, but it does not seem to be appearing.I have this code so far: import UIKitimport MapKitimport CoreLocationclass ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UISearchBarDelegate, UIPopoverPresentationControllerDelegate {var location: CLLocation!let locationManager = CLLocationManager()// Map variablesvar searchController:UISearchController!var annotation:MKAnnotation!var localSearchRequest:MKLocalSearchRequest!var localSearch:MKLocalSearch!var localSearchResponse:MKLocalSearchResponse!var error:NSError!var pointAnnotation:MKPointAnnotation!var pinAnnotationView:MKPinAnnotationView!@IBOutlet weak var mapView: MKMapView!@IBOutlet weak var segmentedControl: UISegmentedControl!@IBOutlet weak var showSearchBar: UIBarButtonItem!@IBOutlet weak var addButton: UIBarButtonItem!@IBOutlet weak var moreStuff: UIBarButtonItem!@IBAction func addButton(sender: AnyObject) {let annotation = MKPointAnnotation()annotation.coordinate = CLLocationCoordinate2D(latitude: self.mapView.userLocation.coordinate.latitude, longitude: self.mapView.userLocation.coordinate.longitude)self.mapView.addAnnotation(annotation)}@IBAction func showSearchBar(sender: UIBarButtonItem!) {searchController = UISearchController(searchResultsController: nil)searchController.hidesNavigationBarDuringPresentation = falseself.searchController.searchBar.delegate = selfpresentViewController(searchController, animated: true, completion: nil)}@IBAction func refresh(sender: AnyObject) {self.locationManager.startUpdatingLocation()}@IBAction func moreStuff(sender: AnyObject) {self.performSegueWithIdentifier("showMoreStuff", sender:self)}override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {if segue.identifier == "showMoreStuff"{var vc = segue.destinationViewController as! UIViewControllervar controller = vc.popoverPresentationControllerif controller != nil{controller?.delegate = self}}}func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {return .None}@IBAction func segmentedControl(sender: UISegmentedControl!) {if sender.selectedSegmentIndex == 0{mapView.mapType = MKMapType.Standard}else if sender.selectedSegmentIndex == 1{mapView.mapType = MKMapType.Satellite}else if sender.selectedSegmentIndex == 2{mapView.mapType = MKMapType.Hybrid}}override func viewDidLoad() {super.viewDidLoad()self.locationManager.delegate = selfself.locationManager.desiredAccuracy = kCLLocationAccuracyBestself.locationManager.requestWhenInUseAuthorization()self.locationManager.startUpdatingLocation()self.mapView.showsUserLocation = true}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}// location delegate methodsfunc locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {let location = locations.lastlet center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))self.mapView.setRegion(region, animated: true)self.locationManager.stopUpdatingLocation()}func locationManager(manager: CLLocationManager, didFailWithError error: NSError){print("Error code: " + error.localizedDescription)}func searchBarSearchButtonClicked(searchBar: UISearchBar){searchBar.resignFirstResponder()dismissViewControllerAnimated(true, completion: nil)if self.mapView.annotations.count != 0{annotation = self.mapView.annotations[0]self.mapView.removeAnnotation(annotation)}localSearchRequest = MKLocalSearchRequest()localSearchRequest.naturalLanguageQuery = searchBar.textlocalSearch = MKLocalSearch(request: localSearchRequest)localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void inif localSearchResponse == nil{let alertController = UIAlertController(title: nil, message: "No Such Place", preferredStyle: UIAlertControllerStyle.Alert)alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, handler: nil))self.presentViewController(alertController, animated: true, completion: nil)return}self.pointAnnotation = MKPointAnnotation()self.pointAnnotation.title = searchBar.textself.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude: localSearchResponse!.boundingRegion.center.longitude)self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)self.mapView.centerCoordinate = self.pointAnnotation.coordinateself.mapView.addAnnotation(self.pinAnnotationView.annotation!)}}}Any help is appreciated :) I am trying to learn the Swift language myself


You would like to show view controller in UIPopover via segue ?

2019年04月19日24分52秒

You don't need to use 'self.' in Swift, unless you're inside a closure. It would make your code look a bit cleaner.

2019年04月19日24分52秒

Where does this code go specifically? I am new to Swift sorry :(

2019年04月20日24分52秒

Specifically the first section of code, I am not sure where to put this

2019年04月19日24分52秒

First section should be placed in viewController that is using UIPopoverPresentationControllerDelegate

2019年04月20日24分52秒

Thanks for your reply. I have only 1 viewcontroller.swift file, when I place the code in there I get an unexpected declaration under the "-". Am I doing something wrong?

2019年04月20日24分52秒

i edit my answer for swift version

2019年04月19日24分52秒