搜索栏点击崩溃

时间:2016-01-08 18:25:42

标签: ios swift

在UIViewController中点击搜索栏文本字段时,应用程序崩溃

我的ViewController看起来像这样 -

class DetailViewController: UIViewController, GMSMapViewDelegate, UISearchBarDelegate {

let testBaseUrl = "https://maps.googleapis.com/maps/api/geocode/json?"

var searchBar = UISearchBar()
var searchImageView   : UIImageView!
var mapView: GMSMapView!
var markers = [GMSMarker]()
var cameraPosition: GMSCameraPosition!
var searchAddress: String!

var mapButton = UIBarButtonItem()
var listButton = UIBarButtonItem()
var filterButton = UIBarButtonItem()
var arrayOfButtons = [AnyObject]()

override func viewDidLoad() {
    super.viewDidLoad()

    cameraPosition = GMSCameraPosition.cameraWithLatitude(-37.809487,
        longitude:144.965699, zoom:17.5, bearing:30, viewingAngle:40)
    mapView = GMSMapView.mapWithFrame(CGRectZero, camera:cameraPosition)

    mapView.delegate = self
    self.view = mapView

}

override func viewWillAppear(animated: Bool) {
    setUISearchView()

}

//MARK: SearchBar Methods

func setUISearchView(){

    let searchImage = UIImage(named: "search")!

    searchImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: searchImage.size.width, height: searchImage.size.height))
    searchImageView.image = searchImage

    let rightGestureRecognizer:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "showSearchBar")
    searchImageView.addGestureRecognizer(rightGestureRecognizer)

    navigationItem.rightBarButtonItem?.customView = searchImageView

}

func setUICancelView(){

    let searchImage = UIImage(named: "cancel")!

    searchImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: searchImage.size.width, height: searchImage.size.height))
    searchImageView.image = searchImage

    let rightGestureRecognizer:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "hideSearchBar")
    searchImageView.addGestureRecognizer(rightGestureRecognizer)

    navigationItem.rightBarButtonItem?.customView = searchImageView

}


func searchButtonPressed(sender: UIBarButtonItem) {
    showSearchBar()
}

func showSearchBar() {

    searchBar.delegate = self
    searchBar.searchBarStyle = UISearchBarStyle.Minimal

    let textFieldInsideSearchBar = searchBar.valueForKey("searchField") as? UITextField

    textFieldInsideSearchBar?.textColor = UIColor.whiteColor()

    UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).tintColor = UIColor.redColor()

    searchBar.alpha = 0
    navigationItem.titleView = searchBar

    UIView.animateWithDuration(0.5, animations: {
        self.searchBar.alpha = 1
        self.setUICancelView()
        }, completion: { finished in
            self.searchBar.becomeFirstResponder()
    })
}

func hideSearchBar() {
    setSearchBarTitle(searchAddress!)

    searchImageView.alpha = 0


    UIView.animateWithDuration(0.3, animations: {

        self.searchBar.hidden =  false
        self.searchBar.alpha = 0
        self.setUISearchView()
        }, completion: { finished in
            self.searchBar.resignFirstResponder()

    })
}

//MARK: UISearchBarDelegate
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
     print("search is working ")
}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    print("searchBarCancelButtonClicked!")
    hideSearchBar()
}

func searchBarSearchButtonClicked(searchBar: UISearchBar) {

    hideSearchBar()

     // Do something..
}

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    print("searchBarTextDidBeginEditing!")
}  

func setSearchBarTitle(streetAddress: NSString){
    //Adding Title Label
    let navigationTitlelabel = UILabel(frame: CGRectMake(0, 0, 200, 21))
    navigationTitlelabel.center = CGPointMake(160, 284)
    navigationTitlelabel.textAlignment = NSTextAlignment.Center
    navigationTitlelabel.textColor  = UIColor.whiteColor()
    navigationTitlelabel.text = "streetAddress" as! String
    self.navigationController!.navigationBar.topItem!.titleView = navigationTitlelabel
}

func setSearchBar(){

    // Remove the drop shadow from the navigation bar for distinct status bar
    navigationController!.navigationBar.clipsToBounds = true

    navigationController!.navigationBar.barTintColor = UIColor.candyGreen()
}


//MARK: Other logic

}

我的场景看起来像这样 - enter image description here

点击搜索栏编辑文本字段时,应用在设备上崩溃。虽然,如果我在showSearcBar()中注释self.searchBar.becomeFirstResponder(),导致没有输入键,它可以正常工作。相同的代码在模拟器上工作正常,因为我可以使用我的开发机器密钥进入搜索栏字段。

我对快速发展很陌生,之前并不了解Obj C.

  • 一旦我点击搜索栏编辑字段,所有UI控件都会从屏幕上消失,只留下谷歌地图*

修改

崩溃日志:

Jan 10 23:33:33 Atul-Kaushiks-iPhone SpringBoard[54] <Notice>: CoreLocation: CLPocketStateService 43328.265701: Stationary,1,Orientation,2,Proximity,0,State,1
Jan 10 23:33:35 Atul-Kaushiks-iPhone SpringBoard[54] <Notice>: CoreLocation: CLPocketStateService 43329.719682: Stationary,2,Orientation,2,Proximity,0,State,2
Jan 10 23:33:39 Atul-Kaushiks-iPhone SpringBoard[54] <Notice>: CoreLocation: CLPocketStateService 43333.481776: Stationary,1,Orientation,2,Proximity,0,State,1
Jan 10 23:33:39 Atul-Kaushiks-iPhone SpringBoard[54] <Notice>: CoreLocation: CLPocketStateService 43333.757166: Stationary,1,Orientation,1,Proximity,0,State,3
Jan 10 23:33:41 Atul-Kaushiks-iPhone SpringBoard[54] <Warning>: LICreateIconForImage passed NULL CGImageRef image
Jan 10 23:33:41 Atul-Kaushiks-iPhone SpringBoard[54] <Notice>: CoreLocation: CLPocketStateService 43336.076814: Stationary,1,Orientation,2,Proximity,0,State,1
Jan 10 23:33:42 Atul-Kaushiks-iPhone SpringBoard[54] <Warning>: HW kbd: Failed to set (null) as keyboard focus
Jan 10 23:33:42 Atul-Kaushiks-iPhone SpringBoard[54] <Notice>: CoreLocation: CLPocketStateService 43336.927797: Stationary,2,Orientation,2,Proximity,0,State,2
Jan 10 23:33:50 Atul-Kaushiks-iPhone SpringBoard[54] <Warning>: [MPUSystemMediaControls] Disabling lock screen media controls updates for screen turning off.
Jan 10 23:33:50 Atul-Kaushiks-iPhone backboardd[56] <Notice>: [HID] [MT] MTSimpleHIDManager::setPropertyInternal detection mode: 3->255 
Jan 10 23:33:50 Atul-Kaushiks-iPhone kernel[0] <Notice>: AppleKeyStore: operation failed (pid: 54 sel: 26 ret: e00002e2 '-536870174')
Jan 10 23:33:50 Atul-Kaushiks-iPhone UserEventAgent[23] <Error>:  LockStateNotifier aksNotificationCallback posting notification: com.apple.mobile.keybagd.lock_status
Jan 10 23:33:50 Atul-Kaushiks-iPhone UserEventAgent[23] <Notice>: (Note ) PIH: Lock status changed.
Jan 10 23:33:51 Atul-Kaushiks-iPhone MobileMail[165] <Warning>: Key bag transitioning from unlocked to locking
Jan 10 23:33:51 Atul-Kaushiks-iPhone UserEventAgent[23] <Warning>: [autosu error]: SPI for AutoSU: unable to find bestSuStart or bestSuEnd with corresponding prob above desired threshold
Jan 10 23:33:51 Atul-Kaushiks-iPhone com.apple.CDScheduler[23] <Error>: AutoSu doesn't have any prediction yet
Jan 10 23:33:51 Atul-Kaushiks-iPhone com.apple.CDScheduler[23] <Error>: Failed to get device restart forecast
Jan 10 23:33:51 Atul-Kaushiks-iPhone SpringBoard[54] <Warning>: [MPUSystemMediaControls] Updating supported commands for now playing application.
Jan 10 23:33:51 Atul-Kaushiks-iPhone wirelessproxd[55] <Notice>: (Error) updateScanner - central is not powered on: 4
Jan 10 23:34:00 Atul-Kaushiks-iPhone kernel[0] <Notice>: AppleKeyStore:Sending lock change 1 for handle 0
Jan 10 23:34:00 Atul-Kaushiks-iPhone UserEventAgent[23] <Error>:  LockStateNotifier aksNotificationCallback posting notification: com.apple.mobile.keybagd.lock_status
Jan 10 23:34:00 Atul-Kaushiks-iPhone UserEventAgent[23] <Notice>: (Note ) PIH: Lock status changed.
Jan 10 23:34:00 Atul-Kaushiks-iPhone MobileMail[165] <Warning>: Key bag transitioning from locking to locked
Jan 10 23:34:13 Atul-Kaushiks-iPhone syncdefaultsd[815] <Notice>: (Note ) marked "com.me.keyvalueservice" topic as "enabled" on <APSConnection: 0x12ee0b070>

3 个答案:

答案 0 :(得分:2)

变量searchAddress被声明为隐式展开的可选。

单击搜索栏按钮时,将调用方法hideSearchBar()并解包searchAddress。这导致崩溃,因为searchAddressnil

解决方案:将searchAddress声明为非可选项,并使用空字符串初始化它。

var searchAddress = ""

并删除感叹号

setSearchBarTitle(searchAddress)

答案 1 :(得分:2)

Crash

我尝试使用你的代码,当我点击取消时,应用程序崩溃,我发现searchAddress是零,但你是setSearchBarTitle(searchAddress!)&#39;,所以崩溃。

我建议将searchAddress设置为可选值,即可。

答案 2 :(得分:1)

我在删除Google地图视图时自行尝试了您的代码,并没有遇到任何崩溃。

Google地图视图和UISearchField可能存在问题,因此请尝试排除self.view = mapView 暂时删除 Google地图视图,并使用评论部分提供反馈是否有效下方。

相关问题