2016-03-02 29 views
0

我正在制作一个小型地图类型的应用程序。我希望在单击按钮“morestuff”后显示弹出式菜单,但似乎没有出现。swift中的UIPopoverPresentationController

我到目前为止这样的代码:

import UIKit 
import MapKit 
import CoreLocation 

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UISearchBarDelegate, UIPopoverPresentationControllerDelegate { 

var location: CLLocation! 
let locationManager = CLLocationManager() 

// Map variables 
var 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 = false 
    self.searchController.searchBar.delegate = self 
    presentViewController(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! UIViewController 

     var controller = vc.popoverPresentationController 

     if 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 = self 

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 

    self.locationManager.requestWhenInUseAuthorization() 

    self.locationManager.startUpdatingLocation() 

    self.mapView.showsUserLocation = true 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// location delegate methods 

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

    let location = locations.last 

    let 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.text 
    localSearch = MKLocalSearch(request: localSearchRequest) 
    localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void in 

     if 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.text 
     self.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.coordinate 
     self.mapView.addAnnotation(self.pinAnnotationView.annotation!) 
    } 
} 

} 

任何帮助表示赞赏:)我努力学习雨燕语言自己

+0

您想通过segue在UIPopover中显示视图控制器吗? –

+0

你不需要使用'自我'。在Swift中,除非你在关闭中。这会让你的代码看起来更清洁一些。 – cjnevin

回答

1

首先你的UIButton应与使用酥料饼的视图控制器来目前作为Popover赛格型。

比添加 UIPopoverPresentationControllerDelegate的委托方法:

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller { 

    return UIModalPresentationNone; 
} 

斯威夫特版本:发件人:

func adaptivePresentationStyleForPresentationController(PC: UIPresentationController!) -> UIModalPresentationStyle { 
    // This *forces* a popover to be displayed on the iPhone 
    return .None 
} 

是添加prepareForSegue后到你的代码

if ([segue.identifier isEqualToString:@"showMoreStuff"]) { 
    UIViewController *dvc = segue.destinationViewController; 
    UIPopoverPresentationController *controller = dvc.popoverPresentationController; 
    if (controller) { 
     controller.delegate = self; 
    } 
} 
+0

此代码的具体含义在哪里?我是新来的迅速对不起:( – Oscar

+0

具体的代码的第一部分,我不知道在哪里把这 – Oscar

+1

第一部分应该放在viewController是使用UIPopoverPresentationControllerDelegate –

相关问题