在实例化UINavigationController之后,UISearchbar不可见

时间:2015-09-10 23:01:48

标签: ios swift uinavigationcontroller uisearchbar uisearchbardelegate

当我点击searchButton时,我加载了UISearchBar。因此,我有func searchBarSearchButtonClicked(searchBar: UISearchBar)func searchBarCancelButtonClicked(searchBar: UISearchBar)委托方法。然而问题是,当我转到另一个嵌入了UINavigationController的视图控制器时,我回到带有搜索栏的页面,然后点击搜索按钮,整个标题视图不会出现。这是我目前的代码:

func searchbarPopulate() {
        tempSearchBar = searchButton
        searchBar2.delegate = self
        searchBar2.searchBarStyle = UISearchBarStyle.Minimal
        searchBar2.showsCancelButton = true
    }

    @IBAction func tapSearchButon(sender: AnyObject) {
        self.viewDidLoad()
        searchbarPopulate()
        self.navigationItem.rightBarButtonItem = nil
        navigationItem.titleView = searchBar2
        searchBar2.alpha = 0
        navigationItem.setLeftBarButtonItem(nil, animated: true)
        UIView.animateWithDuration(0.5, animations: {
//            self.searchBar2.alpha = 1
            }, completion: { finished in
                self.searchBar2 .becomeFirstResponder()
                self.searchBar2.alpha = 1
            })
    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        attractionsMap.removeAnnotations(attractionsMap.annotations);
        performYelpSearch(searchBar.text)
        searchBarCancelButtonClicked(searchBar)
    }
    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            searchBar.alpha = 0
            }) { (completed) -> Void in
//                self.navigationItem.titleView = "Attractions"
                // Make Title View of Attractions in Sketch
                self.navigationItem.rightBarButtonItem = self.searchButton

        }
}

这是当针脚注释被点击并且切换到吸引力详细信息时:

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    if (control == view.rightCalloutAccessoryView) {
        let selectedLocation = view.annotation;
        let selectedCoordinate = view.annotation.coordinate;
        var latitude = selectedCoordinate.latitude
        var longitude = selectedCoordinate.longitude
        var location:CLLocation = CLLocation(latitude: latitude, longitude: longitude)
        let businessPlacemark = MKPlacemark(coordinate: selectedCoordinate, addressDictionary: nil)
        indicatedMapItem = selectedCoordinate;
        let resturantMock:Resturant = Resturant(dictionary: resultQueryDictionary)
        let dataArray = resultQueryDictionary["businesses"] as! NSArray
        var foundDisplayAddress:String = "Address not found"
        for business in dataArray {
            let obj = business as! NSDictionary
            var yelpBusinessMock: YelpBusiness = YelpBusiness(dictionary: obj)
            if yelpBusinessMock.latitude == view.annotation.coordinate.latitude {
                if yelpBusinessMock.longitude == view.annotation.coordinate.longitude {
                    attractionDict = obj;
                    foundDisplayAddress = yelpBusinessMock.displayAddress
                    businessMock = Business(dictionary: obj)
                }
            }
        }
        attractionLocationString = foundDisplayAddress
        performSegueWithIdentifier("attractionToDetail", sender: view);
    }
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "attractionToDetail" {
        if let annotation = (sender as? MKAnnotationView)?.annotation {
            if let ivc = segue.destinationViewController as? AttractionsDetailViewController {
                searchBarCancelButtonClicked(searchBar2)
                ivc.attractionLocation = self.indicatedMapItem
                ivc.attractionDetailAddressString = self.attractionLocationString
                ivc.currentBusiness = self.attractionDict
                ivc.businessToUse = self.businessMock
            }
        }
    }

以下是为清晰起见而发生的事情的gif:
enter image description here

1 个答案:

答案 0 :(得分:1)

确保正确设置navigationItem.titleView。您将titleView设置为nil