2016-05-03 12 views
2

Ich versuche, eine linke Taste zu meiner Navigationscontroller Navigationsleiste hinzuzufügen, aber ich habe den Fehler:Einstellung Ziel für Navigationsleiste Taste

Cannot convert value of type 'NSObject ->() -> LocationViewController' to expected argument type 'AnyObject?'.

ich durch andere SO Fragen und sie zum größten Teil ausgesehen habe sagen um eine Navigationsleiste wie diese einzurichten. Jede Hilfe wird sehr geschätzt.

var leftAddBarButtonItem : UIBarButtonItem = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(cancelButtonTapped)) 

Meine Aktion:

func cancelButtonTapped(sender:UIButton) { 
    self.dismissViewControllerAnimated(true, completion: nil) 
} 

EDIT: Fullview-Controller-Code

import UIKit 
import MapKit 

protocol HandleMapSearch { 
func dropPinZoomIn(placemark:MKPlacemark) 
} 

class LocationViewController: UIViewController { 

let locationManager = CLLocationManager() 
var resultSearchController:UISearchController? = nil 
var selectedPin:MKPlacemark? = nil 

var leftAddBarButtonItem : UIBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelButtonTapped") 

func cancelButtonTapped(sender:UIButton) { 
    self.dismissViewControllerAnimated(true, completion: nil) 
} 

func setLocation() { 
    if let selectedPin = selectedPin { 
     let mapItem = MKMapItem(placemark: selectedPin) 
     let launchOptions = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving] 
     mapItem.openInMapsWithLaunchOptions(launchOptions) 
    } 
} 

@IBOutlet weak var mapView: MKMapView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.navigationController?.navigationItem.setLeftBarButtonItem(leftAddBarButtonItem, animated: true) 

    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.requestWhenInUseAuthorization() 
    locationManager.requestLocation() 

    let locationSearchTable = storyboard!.instantiateViewControllerWithIdentifier("LocationSearchTableViewController") as! LocationSearchTableViewController 
    resultSearchController = UISearchController(searchResultsController: locationSearchTable) 
    resultSearchController?.searchResultsUpdater = locationSearchTable 

    let searchBar = resultSearchController!.searchBar 
    searchBar.sizeToFit() 
    searchBar.placeholder = "Search or Drop a Pin" 
    navigationItem.titleView = resultSearchController?.searchBar 

    resultSearchController?.hidesNavigationBarDuringPresentation = false 
    resultSearchController?.dimsBackgroundDuringPresentation = true 
    definesPresentationContext = true 

    locationSearchTable.mapView = mapView 
    locationSearchTable.handleMapSearchDelegate = self 
} 

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

/* 
// MARK: - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 
} 
*/ 
} 

extension LocationViewController : CLLocationManagerDelegate { 
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
    if status == .AuthorizedWhenInUse { 
     locationManager.requestLocation() 
    } 
} 

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    if let location = locations.first { 
     let span = MKCoordinateSpanMake(0.01, 0.01) 
     let region = MKCoordinateRegion(center: location.coordinate, span: span) 
     mapView.setRegion(region, animated: true) 
    } 
} 

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 
    print("error:: \(error)") 
} 
} 

extension LocationViewController: HandleMapSearch { 
func dropPinZoomIn(placemark:MKPlacemark){ 
    // cache the pin 
    selectedPin = placemark 
    // clear existing pins 
    mapView.removeAnnotations(mapView.annotations) 
    let annotation = MKPointAnnotation() 
    annotation.coordinate = placemark.coordinate 
    annotation.title = placemark.name 
    if let city = placemark.locality, 
     let state = placemark.administrativeArea { 
     annotation.subtitle = "\(city) \(state)" 
    } 
    mapView.addAnnotation(annotation) 
    let span = MKCoordinateSpanMake(0.01, 0.01) 
    let region = MKCoordinateRegionMake(placemark.coordinate, span) 
    mapView.setRegion(region, animated: true) 
} 
} 

extension LocationViewController : MKMapViewDelegate { 
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?{ 
    if annotation is MKUserLocation { 
     //return nil so map view draws "blue dot" for standard user location 
     return nil 
    } 
    let reuseId = "pin" 
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView 
    pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
    pinView?.pinTintColor = UIColor.orangeColor() 
    pinView?.canShowCallout = true 
    let smallSquare = CGSize(width: 60, height: 60) 
    let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare)) 
    button.setBackgroundImage(UIImage(named: "PinButton2.jpg"), forState: .Normal) 
    button.addTarget(self, action: #selector(LocationViewController.setLocation), forControlEvents: .TouchUpInside) 
    pinView?.leftCalloutAccessoryView = button 
    return pinView 
} 
} 
+0

7.3 und ok Stand Frage. Lassen Sie mich wissen, wenn es etwas spezifischeres gibt, das ich einschließe – m1234

+0

Fehler ist immer noch – m1234

+0

Es ist in LocationViewController – m1234

Antwort

3

Ich würde sagen, dass es ein Fehler ist .. lo oks wie Sie Ihre var leftAddBarButtonItem ... Line Out-Seite jede Funktion ... setzen Sie es in viewDidLoad und es sollte funktionieren.

Wenn Sie swift 2.2 als Ihre Aktion #selector(cancelButtonTapped(_:)) sonst einfach sollte verwenden sein "cancelButtonTapped:" verwenden ...

1

verwenden:

let leftButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "btn_clicked:") 
    self.navigationController?.navigationItem.leftBarButtonItem = leftButton 
+0

Fehler ist immer noch da – m1234

Verwandte Themen